Symfony logo

Symfony and PHP attributes

By Jon92 | Symfony & PHP | 4 Apr 2021


The last stable version of Symfony : 5.2 now uses new PHP 8.0 feature : attributes. 

PHP Attributes


PHP attributes offer a way to add metadata information on classes, methods, parameters...

Here is how you can create an attribute :

#[\Attribute]
class MySuperAttribute
{
}

and use it on another class : 

#[MySuperAttribute]
class MyClass
{
    public function aSuperMethod(): string
    {
         return 'Hello';
    }
}

Then, you can fetch the attribute by using PHP Reflection API, and do whatever process you want :

$reflection = new \ReflectionClass(get_class($instance_of_my_class));
$classAttributes = $reflection->getAttributes();

if (!empty($classAttributes) && ($classAttributes[0])->newInstance() instanceof MySuperAttribute) {
    // This way you can get an instance of MySuperAttribute
    $attribute = $classAttributes[0])->newInstance();
}

 

Ok, it's cool but what can I use it for ? 

 

Symfony implementation

 

As attributes give metadata, to classes, methods etc, they can be used to replace traditionnal annotations. 
Now that they are implemented in Symfony you can write a routing annotation using attributes instead of annotations :

 

 namespace App\Controller;

 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\Routing\Annotation\Route;

 class MyController extends AbstractController
 {
     #[Route('/page', name: 'my_page')]
     public function page()
     {
          // ...
     }
 }

 

You can also use them to define Symfony constraints : 

  namespace App\Entity;

  use Symfony\Component\Validator\Constraints as Assert;

  class Book
  {
      #[Assert\Choice(
          choices: ['Fantasy, 'Adventure', 'Thriller'],
          message: 'Please choose a valid category.',
      )]
      private string $category;
  }

 

Is it really better than using annotations ? 

 

Annotations are only PHP docblocks that are parsed by third party code. On the other hand, attributes are parsed natively by PHP : so it increases the performance of your code. Moreover, annotations were only a trick to provide metada on PHP code, but initially docblocks are designed to contain documentation : by moving your annotations in attributes you improve the readability of your code. 

But, don't worry if you do want to use annotations, Symfony 5.2 still handles this way to define routing and constraints.

Next

For the moment, attributes are not supported by standard doctrine annotations, but the work is in progress and when the version 2.9 of Doctrine will be released you will be able to use attributes on your entities' doctrine mapping : https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/attributes-reference.html

How do you rate this article?

3



Symfony & PHP
Symfony & PHP

A blog about Symfony and PHP development

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.