アトリビュートの文法

アトリビュートの文法は、いくつかの重要な要素で構成されています。 アトリビュートの宣言は #[ で始まり、] で終わります。 その内部には、1つ以上のアトリビュートをカンマで区切って並べることができます。 アトリビュート名は、名前空間の基礎 で説明されているように、 非修飾名、修飾名、または完全修飾名にすることができます。 アトリビュートへの引数はオプションで、括弧 () で囲みます。 引数にはリテラル値または定数式のみを指定できます。 位置引数と名前付き引数の両方の構文がサポートされています。

アトリビュート名とその引数はクラスに解決され、 リフレクション API を介してアトリビュートのインスタンスが要求されると、 引数がそのコンストラクタに渡されます。 したがって、各アトリビュートに対してクラスを導入することをお勧めします。

例1 アトリビュートの文法

<?php
// a.php
namespace MyExample;

use Attribute;

#[Attribute]
class MyAttribute
{
    const VALUE = 'value';

    private $value;

    public function __construct($value = null)
    {
        $this->value = $value;
    }
}

// b.php

namespace Another;

use MyExample\MyAttribute;

#[MyAttribute]
#[\MyExample\MyAttribute]
#[MyAttribute(1234)]
#[MyAttribute(value: 1234)]
#[MyAttribute(MyAttribute::VALUE)]
#[MyAttribute(array("key" => "value"))]
#[MyAttribute(100 + 200)]
class Thing
{
}

#[MyAttribute(1234), MyAttribute(5678)]
class AnotherThing
{
}
add a note

User Contributed Notes 1 note

up
3
yarns dot purport0n at icloud dot com
2 years ago
It wasn't obvious to me for a while but you can subclass attributes

https://3v4l.org/TrMTe

<?php

#[Attribute(Attribute::TARGET_PROPERTY)]
class PropertyAttributes
 {
    public function __construct(
        public readonly ?string $name = null,
        public readonly ?string $label = null,
    ) {}
 }

#[Attribute(Attribute::TARGET_PROPERTY)]
class IntegerPropertyAttributes extends PropertyAttributes
 {
    public function __construct(
        ?string $name = null,
        ?string $label = null,
        public readonly ?int $default = null,
        public readonly ?int $min = null,
        public readonly ?int $max = null,
        public readonly ?int $step = null,
    ) {
        parent::__construct($name, $label);
    }
 }

#[Attribute(Attribute::TARGET_PROPERTY)]
class FloatPropertyAttributes extends PropertyAttributes
 {
    public function __construct(
        ?string $name = null,
        ?string $label = null,
        public readonly ?float $default = null,
        public readonly ?float $min = null,
        public readonly ?float $max = null,
    ) {
        parent::__construct($name, $label);
    }
 }

class MyClass
 {
     #[IntegerPropertyAttributes('prop', 'property: ', 5, 0, 10, 1)]
     public int $prop;
 }

$refl = new ReflectionProperty('MyClass', 'prop');
$attributes = $refl->getAttributes();

    foreach ($attributes as $attribute) {
       var_dump($attribute->getName());
       var_dump($attribute->getArguments());
       var_dump($attribute->newInstance());
    }
?>
To Top