PHP 8 attributes in 2023

PHP 8 introduced the attributes, in lieu of the phpdoc comments. They may be added to classes, methods, functions, parameters, properties and class constants and provide a PHP-way to write custom configurations. This is a way to provide contextual information inside the code, and make it available to the PHP engine itself.

30 months later, how are the attributes doing? Let’s take a look in the open source world, where the code may be publicly audited.

Code penetration

Out of 2900 open source project tested, 137 were sporting one or more attributes. This makes about 4% of all tested projects. For comparison, the spaceship operator <=> is currently adopted by 6%, and started many years ago.

Note: last year was about 16% adoption, with a much smaller corpus.

Popular attributes

Here are the most popular attributes, in order of usage occurrences :

  1. Package
  2. ReturnTypeWillChange
  3. Route
  4. Attribute
  5. Pure
  6. AsCommand
  7. Tuleap\Plugin\ListeningToEventClass
  8. IsGranted
  9. Tuleap\Plugin\ListeningToEventName
  10. ParamConverter

A total of 175 different attributes were found.

PHP attributes

In PHP 8.2 and 8.3 (so far), there are four attributes.

  • Attribute
  • ReturnTypeWillChange
  • SensitiveParameter
  • AllowDynamicProperties

They all appear in the top 10, except for SensitiveParameter.

Attributes lists

Last year, we reported attributes lists, edited by independant editors, such as PHPStorm, Navarr, PHP Language Extensions.

Except for Pure, which is in the Top 5, those attributes are used but with a very low level of popularity.

No static analysis tool provide a list of supported attributes. They usually rely comment-based configuration, which may be adapted down to any line of code, rather than selected definitions.

Framework attributes

On the other hand, the most commonly used attributes are the one depending on a framework. This is the case for Package, from Shopware, or Route, from Symfony, AsCommand from Contao, or Tuleap\* from Tuleap (sic).

In fact, rare are the attributes that are used beyond one or more distinct project, unless they are closely related, such as a module or an application using a framework.

In fact, the three most common PHP attributes per distinct projects are AllowDynamicProperties, Attribute and ReturnTypeWillChange. The next ones are AsCommand and ArrayShape, with a much lower level of usage.

Attributes types

The vast majority of attributes are set on the class level, and then, at the method levels, including functions, closures and arrow functions. Argument level also exist, though are the rarest form.

Property and class constants attributes are not reported in this study. In fact, during the writing of this article, I also discovered that enum, interfaces, traits and enum’s cases may also have attributes. This is not documented so far.

Projects using attributes

Here are the OSS projects using the most attributes :

Future of the Attributes

Attributes are still in the adoption phase. The main publishers of attributes are PHP itself, for its own usage, and frameworks editors. Static analysis is lagging in terms of adoption, by lack of standard to describe common information.