<?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 => 'es',
  ),
  'this' => 
  array (
    0 => 'domxpath.registerphpfunctions.php',
    1 => 'DOMXPath::registerPhpFunctions',
    2 => 'Registra una funci&oacute;n PHP como funci&oacute;n 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' => 'es',
    '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">Registra una función PHP como función XPath</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description">
  <h3 class="title">Descripción</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">
   Este método activa la posibilidad de utilizar funciones PHP en expresiones XPath.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters">
  <h3 class="title">Parámetros</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">restrict</code></dt>
     <dd>
      <p class="para">
       Utilice este parámetro únicamente para autorizar ciertas funciones
       a ser llamadas desde XPath.
      </p>
      <p class="para">
       Este parámetro puede ser uno de los siguientes elementos:
       un <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (nombre de función),
       un <span class="type"><a href="language.types.array.php" class="type array">array</a></span> indexado conteniendo nombres de funciones,
       o un <span class="type"><a href="language.types.array.php" class="type array">array</a></span> asociativo con claves correspondientes a nombres de funciones
       y valores asociados correspondientes a <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">Valores devueltos</h3>
  <p class="para">
   No se retorna ningún valor.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-domxpath.registerphpfunctions-errors">
  <h3 class="title">Errores/Excepciones</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Lanza una <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
     el nombre de un callback no es válido.
    </span>
   </li>
   <li class="listitem">
 <span class="simpara">
  Levanta una excepción <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
  <code class="parameter">options</code> contiene una opción inválida.
 </span>
</li>
<li class="listitem">
 <span class="simpara">
  Levanta una excepción <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> si
  <code class="parameter">overrideEncoding</code> utiliza un codificado desconocido.
 </span>
</li>
   <li class="listitem">
    <span class="simpara">
     Lanza una <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span> si
     un callback dado no es llamable.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-domxpath.registerphpfunctions-changelog">
  <h3 class="title">Historial de cambios</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versión</th>
      <th>Descripción</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr id="domxpath.registerphpfunctions..changelog.errors">
       <td>8.4.0</td>
       <td>
        Los nombres de callback inválidos ahora lanzan una
        <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>.
        Pasar una entrada no llamable ahora lanza una
        <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>.
       </td>
     </tr>

     <tr>
      <td>8.4.0</td>
      <td>
       Ahora es posible utilizar <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>s para callbacks
       al utilizar <code class="parameter">restrict</code> con entradas de tipo <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">Ejemplos</h3>
  <p class="para">
   Los siguientes ejemplos utilizan el fichero <var class="filename">book.xml</var>
   que contiene los siguientes datos:
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Ejemplo #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>Ejemplo #2 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> con <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">// Registra el espacio de nombres php (necesario)<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">// Registra las funciones PHP (Sin restricciones)<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">// Llama a la función substr en el título del libro<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"> libros encontrados cuyo título comienza con '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"> por </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>Resultado del ejemplo anterior es similar a:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2 libros encontrados cuyo título comienza con &#039;PHP&#039;:
PHP Basics por Jim Smith
PHP Secrets por Jenny Smythe</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Ejemplo #3 Ejemplo con <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span>
     y <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">// Registra el espacio de nombres php (necesario)<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">// Registra funciones PHP (solo 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">// Retorna true si hay más de un autor<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">// Filtra los libros cuyos autores son múltiples<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">"Libros con múltiples autores :\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>Resultado del ejemplo anterior es similar a:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Libros con múltiples autores :
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Ejemplo #4 <span class="methodname"><strong>DOMXPath::registerPhpFunctions()</strong></span> con 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">// Registrar el namespace php: (necesario)<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">// Registrar las funciones PHP (solo 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">// Filtrar los libros con múltiples autores<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">"Libros con múltiples autores :\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>Resultado del ejemplo anterior es similar a:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Libros con múltiples autores :
PHP Basics</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-domxpath.registerphpfunctions-seealso">
  <h3 class="title">Ver también</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="domxpath.registernamespace.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - Registra el espacio de nombres con el objeto DOMXPath</span></li>
    <li><span class="methodname"><a href="domxpath.query.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - Eval&uacute;a la expresi&oacute;n XPath dada</span></li>
    <li><span class="methodname"><a href="domxpath.evaluate.php" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - Eval&uacute;a una expresi&oacute;n XPath dada y devuelve un resultado tipado si es posible</span></li>
    <li><span class="methodname"><a href="xsltprocessor.registerphpfunctions.php" class="methodname" rel="rdfs-seeAlso">XSLTProcessor::registerPHPFunctions()</a> - Activa el uso de PHP en las hojas de estilo XSLT</span></li>
   </ul>
  </p>
 </div>


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