phpday 2027 - Call for Papers

ReflectionAttribute::getTarget

(PHP 8)

ReflectionAttribute::getTarget — Returns the target on which the attribute is declared

Beschreibung

public function ReflectionAttribute::getTarget(): int

Returns the kind of declaration to which this attribute is applied. This is distinct from the set of targets permitted by the attribute class.

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

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.

Beispiele

Beispiel #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);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(1)
int(8)
int(9)
+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top