3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Требует от поля быть не-пустым при валидации */ #[Attribute] class NotBlank { public function __construct(private readonly string $errorMessage) { } } class Student { #[NotBlank('Имя заполнять кто будет?')] private string $name; } // Получаем студента $student = new Student(); // Получаем объект атрибута $reflProp = new ReflectionProperty(Student::class, 'name'); var_dump($reflProp->getAttributes()); var_dump($reflProp->getAttributes()[0]->newInstance());
Output for 8.4.1 - 8.4.12
array(1) { [0]=> object(ReflectionAttribute)#3 (1) { ["name"]=> string(8) "NotBlank" } } object(NotBlank)#4 (1) { ["errorMessage":"NotBlank":private]=> string(44) "Имя заполнять кто будет?" }
Output for 8.1.30 - 8.1.33, 8.2.22 - 8.2.29, 8.3.0 - 8.3.25
array(1) { [0]=> object(ReflectionAttribute)#3 (0) { } } object(NotBlank)#4 (1) { ["errorMessage":"NotBlank":private]=> string(44) "Имя заполнять кто будет?" }

preferences:
58.6 ms | 408 KiB | 5 Q