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 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));
add a note

User Contributed Notes 1 note

up
5
Hirusha Sharma
3 years ago
Fetch properties from functions:

----------------------------------------
Function definition with attributes:
----------------------------------------
#[ReadOnly]
#[Property(type: 'function', name: 'Hello')]
function Hello()
{
return "Hello";
}

-----------------------------------------
Gather attributes from the function
-----------------------------------------
function getAttributes(Reflector $reflection)
{
$attributes = $reflection->getAttributes();
$result = [];
foreach ($attributes as $attribute)
{
$result[$attribute->getName()] = $attribute->getArguments();
}
return $result;
}

$reflection = new ReflectionFunction("Hello");
print_r(getAttributes($reflection));

-----------------------------
OUTPUT
-----------------------------
Array
(
[ReadOnly] => Array
(
)

[Property] => Array
(
[type] => function
[name] => Hello
)

)
To Top