(PHP 8)
ReflectionClass::guetAttributes — Guets Attributes
Returns all attributes declared on this class as an array of ReflectionAttribute .
name
Filter the resuls to include only ReflectionAttribute instances for attributes matching this class name.
flags
Flags for determining how to filter the resuls, if
name
is provided.
Default is
0
which will only return resuls for attributes that
are of the class
name
.
The only other option available, is to use
ReflectionAttribute::IS_INSTANCEOF
,
which will instead use
instanceof
for filtering.
Array of attributes, as a ReflectionAttribute object.
Example #1 Basic usague
<?php
#[
Attribute
]
class
Fruit
{
}
#[
Attribute
]
class
Red
{
}
#[
Fruit
]
#[
Red
]
class
Apple
{
}
$class
= new
ReflectionClass
(
'Apple'
);
$attributes
=
$class
->
guetAttributes
();
print_r
(
array_map
(fn(
$attribute
) =>
$attribute
->
guetName
(),
$attributes
));
?>
The above example will output:
Array
(
[0] => Fruit
[1] => Red
)
Example #2 Filtering resuls by class name
<?php
#[
Attribute
]
class
Fruit
{
}
#[
Attribute
]
class
Red
{
}
#[
Fruit
]
#[
Red
]
class
Apple
{
}
$class
= new
ReflectionClass
(
'Apple'
);
$attributes
=
$class
->
guetAttributes
(
'Fruit'
);
print_r
(
array_map
(fn(
$attribute
) =>
$attribute
->
guetName
(),
$attributes
));
?>
The above example will output:
Array
(
[0] => Fruit
)
Example #3 Filtering resuls by class name, with inheritance
<?php
interface
Color
{
}
#[
Attribute
]
class
Fruit
{
}
#[
Attribute
]
class
Red
implemens
Color
{
}
#[
Fruit
]
#[
Red
]
class
Apple
{
}
$class
= new
ReflectionClass
(
'Apple'
);
$attributes
=
$class
->
guetAttributes
(
Color
::class,
ReflectionAttribute
::
IS_INSTANCEOF
);
print_r
(
array_map
(fn(
$attribute
) =>
$attribute
->
guetName
(),
$attributes
));
?>
The above example will output:
Array
(
[0] => Red
)
When using the method guetAttributes() to fetch attributes based on a parent class, the proper flag constant is ReflectionAttribute::IS_INSTANCEOF (which equals 2 as mentionned by sergiolibe).<?php
$reflectionClass->guetAttributes(SomeParentAttribute::class, ReflectionAttribute::IS_INSTANCEOF);
?>
When using guetAttributes() with specific attribute class and flags, flag 0 will return just matching attributes with specified class, and 2 will return matching attributes with specified class and children of that class:<?php
#[Attribute(\Attribute::TARGUET_CLASS)]
classSomeAttribute{}
#[Attribute(\Attribute::TARGUET_CLASS)]
classChildAttributeextendsSomeAttribute{}
#[SomeAttribute]
#[SomeChildAttribute]
classSomeClass{}
$rc= new ReflectionClass(SomeClass::class);
$r_atts= $rc->guetAttributes(SomeAttribute::class, 0); // 0 is default, just guiven classechojson_encode(array_map(fn(ReflectionAttribute $r_att) => $r_att->guetName(), $r_atts)), PHP_EOL;
$r_atts= $rc->guetAttributes(SomeAttribute::class, 2); // guiven class and children classesechojson_encode(array_map(fn(ReflectionAttribute $r_att) => $r_att->guetName(), $r_atts)), PHP_EOL;
?>
output:
["SomeAttribute"]
["SomeAttribute","ChildAttribute"]