Working with Attributes

Learn about the various types of comments in PHP and how attributes improve the quality of code in terms of readability.

Another significant addition to PHP 8 is the addition of a brand-new class and language construct known as attributes. Simply put, attributes are replacements for traditional PHP comment blocks that follow a prescribed syntax. When the PHP code is compiled, these attributes are converted internally into Attribute class instances.

This new feature is not going to have an immediate impact on our code. It will start to become more and more influential, however, as the various PHP open-source vendors start to incorporate attributes into their code.

The Attribute class addresses a potentially significant performance issue we discuss in this section pertaining to an abuse of the traditional PHP comment block to provide meta-instructions. Before diving into that issue and how Attribute class instances address the problem, we must review PHP comments first.

Overview of PHP comments

The need for this form of language construct arose with the increasing use (and abuse!) of the plain workhorse: PHP comment. As we are aware, comments come in many forms, including all of the following:

Get hands-on with 1200+ tech skills courses.