PHP attributes: replacing docblock annotations in practice
PHP 8.0 attributes replaced the docblock annotation pattern used by Doctrine, Symfony, and others. Two years in, how complete is the migration in real projects?
Doctrine ORM switched to native attributes in Doctrine 2.13. You can mix docblock and attribute annotations during migration. The new attribute syntax is significantly cleaner for complex mappings.
Symfony routing attributes are stable and the recommended approach in Symfony 6+. No need for a separate YAML or XML routing file for simple controllers.
Custom attribute classes are easy to write and the reflection API for reading them is clean. We replaced a homegrown docblock parser (fragile, slow) with proper attribute classes and reduced the tooling code by 70%.
IDE support for custom attributes is better than docblock annotations. PhpStorm understands attribute types and provides completion and type checking for attribute arguments.
The migration is gradual in most codebases. Doctrine and Symfony accept both forms. No urgency to migrate existing docblock annotations unless you are starting a new project.
Hyperf uses attributes extensively for routing, DI bindings, cron jobs, and middleware. Coming from Laravel where annotations were mostly optional, it took some adjustment.
```php blocks are runnable.