phpday 2027 - Call for Papers

ReflectionAttribute::getTarget

(PHP 8)

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

Опис

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.

Параметри

У цієї функції немає параметрів.

Значення, що повертаються

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.

Приклади

Приклад #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);
?>

Поданий вище приклад виведе:

int(1)
int(8)
int(9)

User Contributed Notes

There are no user contributed notes for this page.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top