<?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());
}
- Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- string(25) "IntegerPropertyAttributes"
array(6) {
[0]=>
string(4) "prop"
[1]=>
string(10) "property: "
[2]=>
int(5)
[3]=>
int(0)
[4]=>
int(10)
[5]=>
int(1)
}
object(IntegerPropertyAttributes)#3 (6) {
["name"]=>
string(4) "prop"
["label"]=>
string(10) "property: "
["default"]=>
int(5)
["min"]=>
int(0)
["max"]=>
int(10)
["step"]=>
int(1)
}
preferences:
177.82 ms | 1396 KiB | 58 Q