(PHP 8)
ReflectionAttribute::getTarget — Returns the target on which the attribute is declared
Returns the kind of declaration to which this attribute is applied. This is distinct from the set of targets permitted by the attribute class.
Questa funzione non ha parametri.
Returns the corresponding
Attribute::TARGET_* constant.
The permitted targets of the attribute class can instead be obtained from
the flags property of its Attribute
instance.
Example #1 Distinguishing actual and permitted attribute targets
<?php
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
class Marker {}
#[Marker]
class Example
{
#[Marker]
public $value;
}
$class = new ReflectionClass(Example::class);
$property = new ReflectionProperty(Example::class, 'value');
var_dump($class->getAttributes(Marker::class)[0]->getTarget());
var_dump($property->getAttributes(Marker::class)[0]->getTarget());
$definition = new ReflectionClass(Marker::class);
var_dump($definition->getAttributes(Attribute::class)[0]->newInstance()->flags);
?>Il precedente esempio visualizzerà:
int(1) int(8) int(9)