<?php #[Attribute] class BaseAttribute { public function __construct(public readonly string $name) { } } #[Attribute] class ConstantlyNameAttribute extends BaseAttribute { public function __construct() { parent::__construct('Chris'); } } #[ConstantlyNameAttribute] class Thing{} $reflectionClass = new ReflectionClass(Thing::class); // Doesn't work var_dump(getName($reflectionClass->getAttributes(BaseAttribute::class))); // Works var_dump(getName($reflectionClass->getAttributes(BaseAttribute::class, ReflectionAttribute::IS_INSTANCEOF))); // Helper function function getName(array $reflectionAttributes) : ?string { if(!count($reflectionAttributes)){ return null; } $attribute = array_pop($reflectionAttributes); return $attribute->newInstance()->name; }
You have javascript disabled. You will not be able to edit any code.