array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'es', ), '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); ?>
To access attributes from classes, methods, functions, parameters, properties and class constants, the Reflection API provides the method getAttributes() on each of the corresponding Reflection objects. This method returns an array of ReflectionAttribute instances that can be queried for attribute name, arguments and to instantiate an instance of the represented attribute.
This separation of reflected attribute representation from actual instance increases control of the programmer to handle errors regarding missing attribute classes, mistyped or missing arguments. Only after calling ReflectionAttribute::newInstance(), objects of the attribute class are instantiated and the correct matching of arguments is validated, not earlier.
Ejemplo #1 Reading Attributes using Reflection API
<?php
#[Attribute]
class MyAttribute
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}
#[MyAttribute(value: 1234)]
class Thing
{
}
function dumpAttributeData($reflection) {
$attributes = $reflection->getAttributes();
foreach ($attributes as $attribute) {
var_dump($attribute->getName());
var_dump($attribute->getArguments());
var_dump($attribute->newInstance());
}
}
dumpAttributeData(new ReflectionClass(Thing::class));
/*
string(11) "MyAttribute"
array(1) {
["value"]=>
int(1234)
}
object(MyAttribute)#3 (1) {
["value"]=>
int(1234)
}
*/
Instead of iterating all attributes on the reflection instance, only those of a particular attribute class can be retrieved by passing the searched attribute class name as argument.
Ejemplo #2 Reading Specific Attributes using Reflection API
<?php
function dumpMyAttributeData($reflection) {
$attributes = $reflection->getAttributes(MyAttribute::class);
foreach ($attributes as $attribute) {
var_dump($attribute->getName());
var_dump($attribute->getArguments());
var_dump($attribute->newInstance());
}
}
dumpMyAttributeData(new ReflectionClass(Thing::class));