Skip to content
AgileLogics
4 min read

Attributes

Prerequisites: Dependency injection & containers

An attribute (PHP 8+) attaches structured metadata to a class, method, or property — PHP's equivalent of a Java annotation or C# attribute, read later via reflection.

#[Attribute]
class Route
{
    public function __construct(public string $path, public string $method = 'GET') {}
}

class UserController
{
    #[Route('/users/{id}', method: 'GET')]
    public function show(int $id): array
    {
        return ['id' => $id];
    }
}

Frameworks like Symfony use attributes exactly like this to map a method to a URL — the framework reads them via reflection at startup rather than needing a separate routes file.