<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.attributes.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'es',
  ),
  'this' => 
  array (
    0 => 'language.attributes.reflection.php',
    1 => 'Lectura de atributos con la API de Reflection',
    2 => 'Lectura de atributos con la API de Reflection',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Atributos',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.syntax.php',
    1 => 'Sintaxis de atributos',
  ),
  'next' => 
  array (
    0 => 'language.attributes.classes.php',
    1 => 'Declaraci&oacute;n de clases de atributos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    'path' => 'language/attributes.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.attributes.reflection" class="sect1">
   <h2 class="title">Lectura de atributos con la API de Reflection</h2>

   <p class="para">
    Para acceder a los atributos de clases, métodos, funciones, parámetros, propiedades
    y constantes de clase, utiliza el método <span class="function"><strong>getAttributes()</strong></span> proporcionado
    por la API de Reflection. Este método devuelve un array de instancias de <span class="classname"><a href="class.reflectionattribute.php" class="classname">ReflectionAttribute</a></span>.
    Estas instancias pueden consultarse para obtener el nombre del atributo, los argumentos, y
    también pueden usarse para instanciar una instancia del atributo representado.
   </p>

   <p class="para">
    Separar la representación reflejada del atributo de su instancia real proporciona un mayor
    control sobre la gestión de errores, como clases de atributos faltantes, argumentos mal escritos,
    o valores ausentes. Los objetos de la clase de atributo se instancian solo después de llamar a
    <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></span>, lo que garantiza que la validación de los argumentos
    se realice en ese momento.
   </p>

   <div class="example" id="example-1">
    <p><strong>Ejemplo #1 Lectura de atributos con la API de Reflection</strong></p>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">#[</span><span style="color: #0000BB">Attribute</span><span style="color: #007700">]<br />class </span><span style="color: #0000BB">MyAttribute<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value </span><span style="color: #007700">= </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />    }<br />}<br /><br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">value</span><span style="color: #007700">: </span><span style="color: #0000BB">1234</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">Thing<br /></span><span style="color: #007700">{<br />}<br /><br />function </span><span style="color: #0000BB">dumpAttributeData</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflection</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$attributes </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">();<br /><br />    foreach (</span><span style="color: #0000BB">$attributes </span><span style="color: #007700">as </span><span style="color: #0000BB">$attribute</span><span style="color: #007700">) {<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">());<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getArguments</span><span style="color: #007700">());<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newInstance</span><span style="color: #007700">());<br />    }<br />}<br /><br /></span><span style="color: #0000BB">dumpAttributeData</span><span style="color: #007700">(new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">Thing</span><span style="color: #007700">::class));<br /></span><span style="color: #FF8000">/*<br />string(11) "MyAttribute"<br />array(1) {<br />  ["value"]=&gt;<br />  int(1234)<br />}<br />object(MyAttribute)#3 (1) {<br />  ["value"]=&gt;<br />  int(1234)<br />}<br />*/</span></span></code></div>
    </div>

   </div>

   <p class="para">
    En lugar de iterar sobre todos los atributos en la instancia de reflexión,
    puedes recuperar solo aquellos de una clase de atributo específica pasando
    el nombre de la clase de atributo como argumento.
   </p>

   <div class="example" id="example-2">
    <p><strong>Ejemplo #2 Lectura de atributos específicos utilizando la API de Reflection</strong></p>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">dumpMyAttributeData</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflection</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$attributes </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">(</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">::class);<br /><br />    foreach (</span><span style="color: #0000BB">$attributes </span><span style="color: #007700">as </span><span style="color: #0000BB">$attribute</span><span style="color: #007700">) {<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">());<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getArguments</span><span style="color: #007700">());<br />       </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newInstance</span><span style="color: #007700">());<br />    }<br />}<br /><br /></span><span style="color: #0000BB">dumpMyAttributeData</span><span style="color: #007700">(new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">Thing</span><span style="color: #007700">::class));</span></span></code></div>
     </div>

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