<?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 => 'it', ), 'this' => array ( 0 => 'language.attributes.reflection.php', 1 => 'Reading Attributes with the Reflection API', ), 'up' => array ( 0 => 'language.attributes.php', 1 => 'Attributes', ), 'prev' => array ( 0 => 'language.attributes.syntax.php', 1 => 'Attribute syntax', ), 'next' => array ( 0 => 'language.attributes.classes.php', 1 => 'Declaring Attribute Classes', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', '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">Reading Attributes with the Reflection API</h2> <p class="para"> To access attributes from classes, methods, functions, parameters, properties, and class constants, use the <span class="function"><strong>getAttributes()</strong></span> method provided by the Reflection API. This method returns an array of <span class="classname"><a href="class.reflectionattribute.php" class="classname">ReflectionAttribute</a></span> instances. These instances can be queried for the attribute name, arguments, and can be used to instantiate an instance of the represented attribute. </p> <p class="para"> Separating the reflected attribute representation from its actual instance provides more control over error handling, such as missing attribute classes, mistyped arguments, or missing values. Objects of the attribute class are instantiated only after calling <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></span>, ensuring that argument validation occurs at that point. </p> <div class="example" id="example-397"> <p><strong>Example #1 Reading Attributes using Reflection API</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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">-></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">-></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">-></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">-></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">-></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"]=><br /> int(1234)<br />}<br />object(MyAttribute)#3 (1) {<br /> ["value"]=><br /> int(1234)<br />}<br />*/</span></span></code></div> </div> </div> <p class="para"> Instead of iterating over all attributes on the reflection instance, you can retrieve only those of a specific attribute class by passing the attribute class name as an argument. </p> <div class="example" id="example-398"> <p><strong>Example #2 Reading Specific Attributes using Reflection API</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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">-></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">-></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">-></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">-></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); ?>