<?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 => 'it',
  ),
  'this' => 
  array (
    0 => 'domxpath.registerphpfunctions.php',
    1 => 'DOMXPath::registerPhpFunctions',
    2 => 'Register PHP functions as XPath functions',
  ),
  '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' => 'en',
    '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">Register PHP functions as XPath functions</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description">
  <h3 class="title">Descrizione</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">
   This method enables the ability to use PHP functions within XPath expressions.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">restrict</code></dt>
     <dd>
      <p class="para">
       Use this parameter to only allow certain functions to be called from XPath.
      </p>
      <p class="para">
       This parameter can be one of the following:
       a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (a function name),
       an indexed <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of function names,
       or an associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> with keys being the function name
       and the associated value being the <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">Valori restituiti</h3>
  <p class="para">
   Nessun valore viene restituito.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-domxpath.registerphpfunctions-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> if
     a callback name is not valid.
    </span>
   </li>
   <li class="listitem">
 <span class="simpara">
  Throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> if
  <code class="parameter">options</code> contains an invalid option.
 </span>
</li>
<li class="listitem">
 <span class="simpara">
  Throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> if
  <code class="parameter">overrideEncoding</code> is an unknown encoding.
 </span>
</li>
   <li class="listitem">
    <span class="simpara">
     Throws a <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span> if
     a given callback is not callable.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-domxpath.registerphpfunctions-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr id="domxpath.registerphpfunctions..changelog.errors">
      <td>8.4.0</td>
      <td>
       Invalid callback names now throws a
       <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>.
       Passing a non-callable entry now throws a
       <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>.
      </td>
     </tr>

     <tr>
      <td>8.4.0</td>
      <td>
       It is now possible to use <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>s for callbacks
       when using <code class="parameter">restrict</code> with <span class="type"><a href="language.types.array.php" class="type array">array</a></span>
       entries.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-domxpath.registerphpfunctions-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   The following examples use <var class="filename">book.xml</var> which contains the following:
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #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>Example #2 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> with <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">// Register the php: namespace (required)<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">// Register PHP functions (no restrictions)<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">// Call substr function on the 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>Il precedente esempio visualizzerà
qualcosa simile a:</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>Example #3 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> with <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">// Register the php: namespace (required)<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">// Register PHP functions (has_multiple only)<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">// Return true if more than one author<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">// Filter books with multiple authors<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>Il precedente esempio visualizzerà
qualcosa simile a:</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>Example #4 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> with a <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">// Register the php: namespace (required)<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">// Register PHP functions (has_multiple only)<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">// Filter books with multiple authors<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>Il precedente esempio visualizzerà
qualcosa simile a:</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">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="domxpath.registernamespace.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - Registers the namespace with the DOMXPath object</span></li>
    <li><span class="methodname"><a href="domxpath.query.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - Evaluates the given XPath expression</span></li>
    <li><span class="methodname"><a href="domxpath.evaluate.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - Evaluates the given XPath expression and returns a typed result if possible</span></li>
    <li><span class="methodname"><a href="xsltprocessor.registerphpfunctions.php" class="methodname" rel="rdfs-seeAlso">XSLTProcessor::registerPHPFunctions()</a> - Enables the ability to use PHP functions as XSLT functions</span></li>
   </ul>
  </p>
 </div>


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