#! Require PHP >= 8.0
#! This is a Sample
<?php
declare(strict_types = 1);
#[Attribute]
class Foo
{
function __construct(){
echo "Running " . __METHOD__ . PHP_EOL;
}
}
#[Attribute(Attribute::TARGET_CLASS|Attribute::IS_REPEATABLE)]
class Bar {
function __construct(?string ...$args){
echo "Running " . __METHOD__ ,
" args: " . implode(", ", $args) . PHP_EOL;
}
}
#[Attribute(Attribute::TARGET_ALL)]
class Baz {
function __construct(
private string $parameter
){
echo "Running " . __METHOD__ ,
" arg: " . $this->parameter . PHP_EOL;
}
}
#[Foo] #[Bar] #[Bar("Banana")] #[Bar("Banana", "Apple", "Lemon", "Grape")] #[Baz("The Only One")] class Qux
{
}
$ref = new ReflectionClass(Qux::class);
$attrs = $ref->getAttributes(); $attrs[0]->newInstance(); $attrs[1]->newInstance(); $attrs[2]->newInstance(); $attrs[3]->newInstance(); $attrs[4]->newInstance();