<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.domxpath.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'domxpath.registerphpfunctions.php',
    1 => 'DOMXPath::registerPhpFunctions',
    2 => 'PHP の関数を XPath 関数として登録する',
  ),
  'up' => 
  array (
    0 => 'class.domxpath.php',
    1 => 'DOMXPath',
  ),
  'prev' => 
  array (
    0 => 'domxpath.registerphpfunctionns.php',
    1 => 'DOMXPath::registerPhpFunctionNS',
  ),
  'next' => 
  array (
    0 => 'enum.dom-adjacentposition.php',
    1 => 'Dom\\AdjacentPosition',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/dom/domxpath/registerphpfunctions.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="domxpath.registerphpfunctions" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMXPath::registerPhpFunctions</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMXPath::registerPhpFunctions</span> &mdash; <span class="dc-title">PHP の関数を XPath 関数として登録する</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMXPath::registerPhpFunctions</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$restrict</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

  <p class="para rdfs-comment">
   このメソッドは、PHP の関数を XPath 式の中で使えるようにします。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">restrict</code></dt>
     <dd>
      <p class="para">
       このパラメータを使って、特定の関数のみを XPath からコールできるように制限することができます。
      </p>
      <p class="para">
       このパラメータは、以下のいずれかになります:
       <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (関数名),
       関数名の <span class="type"><a href="language.types.array.php" class="type array">array</a></span>,
       関数名がキーで <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> な値を持つ連想配列。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-domxpath.registerphpfunctions-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   値を返しません。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-domxpath.registerphpfunctions-errors">
  <h3 class="title">エラー / 例外</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     コールバック名が正しくない場合、　　
     <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> がスローされます。
    </span>
   </li>
   <li class="listitem">
 <span class="simpara">
  <code class="parameter">options</code> が不正なオプションを含む場合、
  <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> をスローします。
 </span>
</li>
<li class="listitem">
 <span class="simpara">
  <code class="parameter">overrideEncoding</code> が未知のエンコーディングである場合、
  <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> をスローします。
 </span>
</li>
   <li class="listitem">
    <span class="simpara">
     指定されたコールバックが callable でない場合、
     <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span> がスローされます。
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-domxpath.registerphpfunctions-changelog">
  <h3 class="title">変更履歴</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>バージョン</th>
      <th>説明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr id="domxpath.registerphpfunctions..changelog.errors">
      <td>8.4.0</td>
      <td>
       コールバック名が正しくない場合、
       <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>
       がスローされるようになりました。
       callable でない値を指定した場合、
       <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>
       がスローされるようになりました。
      </td>
     </tr>

     <tr>
      <td>8.4.0</td>
      <td>
       <code class="parameter">restrict</code> を <span class="type">配列</span> にする際、
       コールバックとして <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> を使用できるようになりました。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-domxpath.registerphpfunctions-examples">
  <h3 class="title">例</h3>
  <p class="para">
   次の例では、以下のような内容の <var class="filename">book.xml</var> を使います。
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 book.xml</strong></p>
    <div class="example-contents">
<div class="annotation-interactive xmlcode"><pre class="xmlcode">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;books&gt;
 &lt;book&gt;
  &lt;title&gt;PHP Basics&lt;/title&gt;
  &lt;author&gt;Jim Smith&lt;/author&gt;
  &lt;author&gt;Jane Smith&lt;/author&gt;
 &lt;/book&gt;
 &lt;book&gt;
  &lt;title&gt;PHP Secrets&lt;/title&gt;
  &lt;author&gt;Jenny Smythe&lt;/author&gt;
 &lt;/book&gt;
 &lt;book&gt;
  &lt;title&gt;XML basics&lt;/title&gt;
  &lt;author&gt;Joe Black&lt;/author&gt;
 &lt;/book&gt;
&lt;/books&gt;</pre>
</div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>例2 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> と <code class="literal">php:functionString</code> の例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$doc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'examples/book-simple.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 名前空間 php: を登録します (必須)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// PHP の関数を登録します (無制限)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPhpFunctions</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// substr 関数を book の title に適用します<br /></span><span style="color: #0000BB">$nodes </span><span style="color: #007700">= </span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:functionString("substr", title, 0, 3) = "PHP"]'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Found </span><span style="color: #007700">{</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">length</span><span style="color: #007700">}</span><span style="color: #DD0000"> books starting with 'PHP':\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$nodes </span><span style="color: #007700">as </span><span style="color: #0000BB">$node</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$title  </span><span style="color: #007700">= </span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$author </span><span style="color: #007700">= </span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"author"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$title</span><span style="color: #DD0000"> by </span><span style="color: #0000BB">$author</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Found 2 books starting with &#039;PHP&#039;:
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>例3 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> と <code class="literal">php:function</code> の例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$doc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'examples/book-simple.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 名前空間 php: を登録します (必須)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// PHP の関数を登録します (has_multiple 限定)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPhpFunctions</span><span style="color: #007700">(</span><span style="color: #DD0000">"has_multiple"</span><span style="color: #007700">);<br /> <br />function </span><span style="color: #0000BB">has_multiple</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// author が複数ある場合に true を返します<br />    </span><span style="color: #007700">return </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) &gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">// 複数の author がいる book のみを取り出します<br /></span><span style="color: #0000BB">$books </span><span style="color: #007700">= </span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:function("has_multiple", author)]'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Books with multiple authors:\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$books </span><span style="color: #007700">as </span><span style="color: #0000BB">$book</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Books with multiple authors:
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>例4 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> と <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> の例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$doc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'examples/book-simple.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 名前空間 php: を登録します (必須)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// PHP の関数を登録します (has_multiple 限定)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPhpFunctions</span><span style="color: #007700">([</span><span style="color: #DD0000">"has_multiple" </span><span style="color: #007700">=&gt; fn (</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) &gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">// 複数の author がいる book のみを取り出します<br /></span><span style="color: #0000BB">$books </span><span style="color: #007700">= </span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:function("has_multiple", author)]'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Books with multiple authors:\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$books </span><span style="color: #007700">as </span><span style="color: #0000BB">$book</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Books with multiple authors:
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-domxpath.registerphpfunctions-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="domxpath.registernamespace.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - DOMXPath オブジェクトの名前空間を登録する</span></li>
    <li><span class="methodname"><a href="domxpath.query.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - 与えられた XPath 式を評価する</span></li>
    <li><span class="methodname"><a href="domxpath.evaluate.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - 与えられた XPath 式を評価し、可能であれば結果を返す</span></li>
    <li><span class="methodname"><a href="xsltprocessor.registerphpfunctions.php" class="methodname" rel="rdfs-seeAlso">XSLTProcessor::registerPHPFunctions()</a> - PHP 関数を XSLT 関数として利用できるようにする</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>