Lesson 4 of 9 · 20m
Attributes: Metadata the Runtime Can Actually Read
How PHP attributes differ from docblocks, how reflection consumes them, and where framework-style metadata helps or hurts architecture.
For fifteen years PHP frameworks put behavior in docblock annotations and pretended comments were configuration. It worked because everyone ran Doctrine Annotations, but it was always a hack: comments were invisible to the engine, easy to typo, and difficult to validate. Attributes are the native replacement. They are real classes, discovered through reflection, and instantiated only when asked.
An attribute is a class with a target
use Attribute;
#[Attribute(Attribute::TARGET_METHOD)]
final readonly class RequiresRole
{
public function __construct(public string $role) {}
}
final class InvoiceController
{
#[RequiresRole('billing-admin')]
public function void(string $id): Response
{
// ...
}
}The attribute declaration is inert metadata. Nothing calls it magically. Some framework, middleware, or your own bootstrap reads it:
$method = new ReflectionMethod(InvoiceController::class, 'void');
$attributes = $method->getAttributes(RequiresRole::class);
foreach ($attributes as $attribute) {
$role = $attribute->newInstance()->role;
}That instantiation step matters. Reflection can tell you an attribute exists without constructing it, which is useful when constructor dependencies are unavailable or arguments might be invalid.
Where attributes are the right tool
Attributes shine when metadata is intrinsic to the declaration: routing on a controller method, serialization groups on a DTO property, validation constraints on input, event listener registration, or a console command name. They fail when behavior needs runtime context or ordering that is easier to express in explicit code.
final class SignupInput
{
public function __construct(
#[Assert\Email]
public string $email,
#[Assert\Length(min: 12)]
public string $password,
) {}
}The value here is colocation. The input shape and its constraints cannot drift into separate files. The cost is coupling to a validation component; decide deliberately whether your domain objects should know framework attributes or whether only transport DTOs should.
Attributes do not replace configuration
If operators tune it per environment, it is configuration. If programmers declare it as part of the type or behavior, it can be an attribute. A rate limit that differs in production belongs in config. “This endpoint requires billing-admin” is usually intrinsic enough to colocate.
Field notes
- Repeated attributes need
Attribute::IS_REPEATABLE; otherwise reflection still shows them but instantiation semantics become confusing. - Attribute arguments are constant expressions. You cannot call services in them — instantiate and interpret them elsewhere.
ReflectionAttribute::newInstance()can throw. Validate attributes during container compilation or boot when possible, not on the first production request.- Cache reflection metadata aggressively in production. Reflection is not ruinously slow, but scanning every class on every request is wasted work.
- Docblocks still matter for static analysis array shapes and generics. Attributes did not replace Psalm/PHPStan annotations.
Was this lesson helpful?