PHP 8.6.0 Alpha 2 available for testing

ReflectionConstant::getAttributes

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

ReflectionConstant::getAttributesGets Attributes

説明

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

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

パラメータ

name
指定したクラス名にマッチするアトリビュートの、 ReflectionAttribute のみを含むようにフィルタします。
flags
name が指定されていた場合に、 結果をどうフィルタするかを決めるフラグ。 デフォルトは 0 です。 この場合、アトリビュートのクラス名が name であるものだけを返します。 他の利用可能なオプションは、 ReflectionAttribute::IS_INSTANCEOF です。 この場合、フィルタリングに instanceof を使います。

戻り値

Array of attributes, as ReflectionAttribute objects.

変更履歴

バージョン 説明
8.5.0 This method was introduced.

例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));
?>

上の例の出力は以下となります。

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

参考

add a note

User Contributed Notes

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