mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 11:36:23 +00:00
789 B
789 B
date | id | title |
---|---|---|
20201110 | 046ba725-37b9-4aca-837c-5d0cda679cc2 | PHP Attributes |
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;
}
}