How can i use php8 attributes instead of annotations in doctrine?

How can i use php8 attributes instead of annotations in doctrine?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Switching from Annotations to Attributes in Doctrine ORM

When you upgrade a PHP project to version 8.0, the most noticeable syntactic shift is the move from docblock annotations to native attributes. If you are trying to replace /** @ORM\Column(type="string") */ with #[ORM\Column(type: "string")], encountering an error like (error: Class 'Column' is not annotated with 'Attribute') usually points to a missing dependency rather than a limitation in Doctrine itself.

The core issue lies in how PHP handles attributes versus annotations. Annotations are parsed by the DocBlock parser, which reads comments above methods or properties and looks for specific keywords. Attributes, however, are native language constructs that exist as actual class objects at runtime. To use #[ORM\Column], your code must know about the Attribute interface defined in PHP 8.0.

If Doctrine does not automatically register its attributes with the PHP runtime, you will hit a fatal error or a missing class exception when the parser encounters the attribute tag. In older versions of Doctrine ORM, the package did not include the necessary doctrine/annotations dependency to handle this specific transition for attributes, or it required explicit configuration to load them as attributes.

To resolve the error Class 'Column' is not annotated with 'Attribute', you need to ensure that the doctrine/orm package is aware of its own attribute classes. In modern versions of Doctrine (2.13 and later), this behavior has changed significantly. You no longer need to manually register attributes in the service configuration if you are using the standard installation, provided your PHP version supports it natively.

However, if you are still seeing errors or if you are on an older version of the ORM, you must explicitly enable attribute support. This is typically done by configuring the AttributeReader in your Doctrine configuration. You can add the following to your config.php or bootstrap file:

use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

// Ensure attributes are loaded
$config->setMetadataDriverImpl(new AnnotationDriver($paths, new AttributeReader()));

More commonly now, if you have upgraded both PHP and Doctrine to their latest stable versions (Doctrine ORM 3.0+), the framework automatically loads attributes via the AttributeReader service container without any manual intervention. The error message suggests that the Attribute class itself is not being recognized as an attribute, which often happens when the doctrine/annotations package is missing or when the AttributeMap is not initialized correctly in legacy setups.

Here is a correct implementation example where attributes are used directly on the entity property:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\Column(type: 'string', length: 255)]
    private string $username;
}

This syntax is cleaner and type-safe, leveraging PHP's native reflection to read the metadata. The trade-off here is version compatibility. If you are maintaining a legacy codebase on PHP 7.x, you cannot use attributes at all; you must stick to annotations or switch to XML/YAML mapping files. Even on PHP 8, if you are running Doctrine ORM 2.x with an older patch level, you might need to explicitly enable attribute support in the EntityManager configuration via the getConfiguration()->setAutoMappingByAttribute(true) method or similar flags depending on the specific version's release notes.

One common pitfall is assuming that simply changing the syntax will work instantly. You must ensure that your composer.json includes the correct versions of doctrine/orm and doctrine/event-manager. Sometimes, the doctrine/annotations package is still required by older ORM versions to handle mixed annotation reading strategies, but for pure attribute usage in modern stacks, it is being phased out.

If you are building a new application using Laravel or Symfony, these frameworks often have specific service providers that automatically configure Doctrine. In such cases, the #[ORM\Column] syntax should work out of the box on PHP 8.0+. If you encounter issues there, check your config/ide-helper.php or similar static analysis tools, as they sometimes generate outdated stubs that expect annotations instead of attributes.

Ultimately, the transition is seamless once the environment is updated. The error you encountered was a symptom of an unconfigured runtime environment where the PHP engine couldn't locate the attribute definitions. By ensuring your Doctrine version matches your PHP 8 capabilities and verifying that the AttributeReader is active in your configuration, the syntax switch becomes a straightforward migration path.

Our network