Reading Attributes with the Reflection API

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 newInstance(), objects of the attribute class are instantiated and the correct matching of arguments is validated, not earlier.

Example #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.

Example #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());
    }
}

dumpAttributeData(new ReflectionClass(Thing::class));

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/language.attributes.reflection.php