<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'domxpath.registerphpfunctions.php',
    1 => 'DOMXPath::registerPhpFunctions',
    2 => 'Enregistre une fonction PHP comme fonction 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' => 'fr',
    '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">Enregistre une fonction PHP comme fonction XPath</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description">
  <h3 class="title">Description</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">
   Cette méthode active la possibilité d&#039;utiliser des fonctions PHP dans des
   expressions XPath.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">restrict</code></dt>
     <dd>
      <p class="para">
       Utiliser ce paramètre uniquement pour autoriser certaines fonctions
       à être appelées depuis XPath.
      </p>
      <p class="para">
       Ce paramètre peut être l&#039;un des éléments suivants :
       une <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (nom de fonction),
       un <span class="type"><a href="language.types.array.php" class="type array">array</a></span> indexé contenant des noms de fonctions,
       ou un <span class="type"><a href="language.types.array.php" class="type array">array</a></span> associatif avec des clés correspondant aux noms de fonctions
       et des valeurs associées correspondant à des <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">Valeurs de retour</h3>
  <p class="para">
   Aucune valeur n&#039;est retournée.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-domxpath.registerphpfunctions-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Lance une <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
     le nom d&#039;un callback n&#039;est pas valide.
    </span>
   </li>
   <li class="listitem">
 <span class="simpara">
  Lève une exception <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
  <code class="parameter">options</code> contient une option invalide.
 </span>
</li>
<li class="listitem">
 <span class="simpara">
  Lève une exception <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
  <code class="parameter">overrideEncoding</code> utilise un encodage inconnu.
 </span>
</li>
   <li class="listitem">
    <span class="simpara">
     Lance une <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span> si
     un callback donné n&#039;est pas appelable.
    </span>
   </li>
  </ul>
 </div>



 <div class="refsect1 changelog" id="refsect1-domxpath.registerphpfunctions-changelog">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr id="domxpath.registerphpfunctions..changelog.errors">
       <td>8.4.0</td>
       <td>
        Les noms de callback invalides lancent maintenant une
        <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>.
        Passer une entrée non appelable lance maintenant une
        <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>.
       </td>
     </tr>

     <tr>
      <td>8.4.0</td>
      <td>
       Il est désormais possible d&#039;utiliser des <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>s pour des callbacks
       lors de l&#039;utilisation de <code class="parameter">restrict</code> avec des entrées de type <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-domxpath.registerphpfunctions-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   Les exemples suivants utilisent le fichier <var class="filename">book.xml</var>
   qui contient les données suivantes :
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #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>Exemple #2 Exemple avec <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span>
     et <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">// Enregistre le namespace php (nécessaire)<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">// Enregistre les fonctions PHP (Aucune restriction)<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">// Appelle la fonction substr sur le titre du livre<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">"</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"> livres trouvés dont le titre commence par '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"> par </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>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2 livres trouvés dont le titre commence par &#039;PHP&#039;:
PHP Basics par Jim Smith
PHP Secrets par Jenny Smythe</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Exemple avec <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span>
     et <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">// Enregistre le namespace php (nécessaire)<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">// Enregistre des fonctions PHP (has_multiple uniquement)<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">// Retourne true s'il y a plus d'un auteur<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">// Filtre les livres dont les auteurs sont multiples<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">"Livres avec plusieurs auteurs :\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>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Livres avec plusieurs auteurs :
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Exemple #4 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> avec un <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">// Enregistrer le namespace php: (nécessaire)<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">// Enregistrer les fonctions PHP (uniquement 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">// Filtrer les livres avec plusieurs auteurs<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">"Livres avec plusieurs auteurs :\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>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Livres avec plusieurs auteurs :
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-domxpath.registerphpfunctions-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="domxpath.registernamespace.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - Enregistre le namespace avec l'objet DOMXPath</span></li>
    <li><span class="methodname"><a href="domxpath.query.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - &Eacute;value l'expression XPath donn&eacute;e</span></li>
    <li><span class="methodname"><a href="domxpath.evaluate.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - &Eacute;value une expression XPath donn&eacute;e et retourne un r&eacute;sultat typ&eacute; si possible</span></li>
    <li><span class="methodname"><a href="xsltprocessor.registerphpfunctions.php" class="methodname" rel="rdfs-seeAlso">XSLTProcessor::registerPHPFunctions()</a> - Active l'utilisation de PHP dans les feuilles de styles XSLT</span></li>
   </ul>
  </p>
 </div>


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