PHP 8.6.0 Alpha 2 available for testing

ReflectionConstant::getAttributes

(No version information available, might only be in Git)

ReflectionConstant::getAttributesGets Attributes

Beschreibung

public function ReflectionConstant::getAttributes(?string $name = null, int $flags = 0): array

Returns all attributes declared on this global constant as an array of ReflectionAttribute.

Parameter-Liste

name

Die Ergebnisse werden so gefiltert, dass nur ReflectionAttribute-Instanzen für Attribute mit diesem Klassennamen enthalten sind.

flags

Flags, die festlegen, wie die Ergebnisse gefiltert werden sollen, wenn name angegeben wird.

Die Voreinstellung ist 0, was nur Ergebnisse für die Attribute der Klasse name liefert.

Die einzige andere Möglichkeit ist die Verwendung von ReflectionAttribute::IS_INSTANCEOF, wodurch stattdessen instanceof zum Filtern verwendet wird.

Rückgabewerte

Array of attributes, as ReflectionAttribute objects.

Changelog

Version Beschreibung
8.5.0 This method was introduced.

Beispiele

Beispiel #1 Basic usage

<?php
#[Attribute]
class Fruit {
}

#[Attribute]
class Red {
}

#[Fruit]
#[Red]
const APPLE = 'apple';

$constant = new ReflectionConstant('APPLE');
$attributes = $constant->getAttributes();
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Array
(
    [0] => Fruit
    [1] => Red
)

add a note

User Contributed Notes

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