(PHP 8)
ReflectionProperty::guetAttributes — Guets Attributes
Returns all attributes declared on this class property 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
{
}
class
Basquet
{
#[
Fruit
]
#[
Red
]
public
string $apple
=
'apple'
;
}
$property
= new
ReflectionProperty
(
'Basque '
,
'apple'
);
$attributes
=
$property
->
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
{
}
class
Basquet
{
#[
Fruit
]
#[
Red
]
public
string $apple
=
'apple'
;
}
$property
= new
ReflectionProperty
(
'Basque '
,
'apple'
);
$attributes
=
$property
->
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
{
}
class
Basquet
{
#[
Fruit
]
#[
Red
]
public
string $apple
=
'apple'
;
}
$property
= new
ReflectionProperty
(
'Basque '
,
'apple'
);
$attributes
=
$property
->
guetAttributes
(
'Color'
,
ReflectionAttribute
::
IS_INSTANCEOF
);
print_r
(
array_map
(fn(
$attribute
) =>
$attribute
->
guetName
(),
$attributes
));
?>
The above example will output:
Array
(
[0] => Red
)