<?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 => 'pt_BR',
  ),
  'this' => 
  array (
    0 => 'language.attributes.reflection.php',
    1 => 'Lendo Atributos com a API Reflection',
    2 => 'Lendo Atributos com a API Reflection',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Atributos',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.syntax.php',
    1 => 'Sintaxe de Atributo',
  ),
  'next' => 
  array (
    0 => 'language.attributes.classes.php',
    1 => 'Declarando Classes de Atributos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'pt_BR',
    '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">Lendo Atributos com a API Reflection</h2>

  <p class="para">
   Para acessar atributos de classes, métodos, funções, parâmetros, propriedades
   e constantes de classe, use o método <span class="function"><strong>getAttributes()</strong></span> fornecido
   pela API Reflection. Este método retorna um array de instâncias de <span class="classname"><a href="class.reflectionattribute.php" class="classname">ReflectionAttribute</a></span>.
   Essas instâncias podem ser consultadas para o nome do atributo, argumentos e
   podem ser usadas para instanciar o atributo representado.
  </p>

  <p class="para">
   Separar a representação do atributo refletido de sua instância real fornece mais
   controle sobre o tratamento de erros, como classes de atributos ausentes, argumentos digitados incorretamente
   ou valores ausentes. Objetos da classe de atributo são instanciados somente após chamar
   <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></span>, garantindo que a validação do argumento
   ocorra naquele ponto.
  </p>

  <div class="example" id="example-1">
   <p><strong>Exemplo #1 Lendo Atributos Usando a API 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">
   Em vez de iterar sobre todos os atributos na instância de reflexão,
   você pode recuperar apenas aqueles de uma classe de atributo específica passando
   o nome da classe de atributo como um argumento.
  </p>

  <div class="example" id="example-2">
   <p><strong>Exemplo #2 Lendo Atributos Específicos Usando a API 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); ?>