Description

Introduced in PHP 8.0. Attributes1 are a way to add metadata to classes. Stitcher2 has an article that goes in depth.

Syntax

use App\Attributes\ExampleAttribute;
 
#[ExampleAttribute]
class Foo
{
    #[ExampleAttribute]
    public const FOO = 'foo';
 
    #[ExampleAttribute]
    public $x;
 
    #[ExampleAttribute]
    public function foo(#[ExampleAttribute] $bar) { }
}
#[Attribute]
class ExampleAttribute
{
    public $value;
 
    public function __construct($value)
    {
        $this->value = $value;
    }
}
 

Footnotes

Footnotes

  1. https://wiki.php.net/rfc/attributes

  2. https://stitcher.io/blog/attributes-in-php-8