Please wait


Attributes, introduced in PHP 8.0, provide a way to add structured, machine-readable metadata to declarations of classes, properties, functions, methods, and parameters. They represent a powerful feature for developers, allowing more declarative code and reducing the reliance on doc-comments for metadata purposes.

Sound confusing?

Attributes are a feature not often used by beginners. They're mostly a feature used by library/framework developers. More often than not, you just need to know how to use them. That doesn't mean you should avoid creating custom attributes, as they're extremely powerful, but don't worry if you can't find a use case for them in your own code.

In Laymen Terms

Imagine attributes as special labels or tags you can attach to parts of your code, like classes or functions. These labels hold extra information about the code, which can be used by other parts of the program to understand or handle that code in specific ways.

Metadata is essentially "data about data." In this context, it refers to additional information that describes specific elements of your code. Just like a label on a food package might tell you the ingredients, nutritional facts, and expiration date, metadata in programming can provide details about how a particular piece of code should behave, be handled, or be understood.

Defining an Attribute

Defining a custom attribute is like creating a special label that you can stick onto different parts of your code. This label can carry specific information or instructions that other parts of the code can read and follow. These are the steps you would take to define a custom attribute.

  1. Create a New Class: A custom attribute is created as a class.
  2. Mark the Class as an Attribute: By putting #[Attribute] before the class definition, you're telling PHP that this class is going to be used as an attribute (or a special label).
  3. Add Properties to the Attribute (Optional): You can define properties (variables) inside the class to store specific information within the attribute. This information can then be read by other parts of your program.

Let's say you want to create a custom attribute called MySpecialLabel that you can use to mark certain parts of your code as special. Here's how you can define it:

class MySpecialLabel {
  // You can add properties here if needed

That's it! You've just defined a custom attribute. You can now use this attribute to label other parts of your code by putting #[MySpecialLabel] before them.

Here's how you might use the MySpecialLabel attribute to label a class as special:

class MyClass {
    // Class code here

Other parts of your program can then detect the presence of this label and react accordingly.

Reading the Attribute Metadata

Reading the attribute added to a class in PHP involves using Reflection, a feature that allows you to introspect or look into details of the class, including its attributes.

What is the Reflection API?

The Reflection API in PHP is a set of classes and functions that allow you to introspect and analyze your code at runtime. It gives you the ability to examine objects, classes, functions, methods, properties, and even attributes, obtaining detailed information about their structure, properties, and behavior.

Reflection is like having a mirror that lets you look inside your code while it's running. You can see details about the classes, methods, properties, and other elements, even if you don't have direct access to them.

These are typically the steps you would take to read attributes.

  1. Import the Required Classes: You'll need to use the ReflectionClass class to inspect the details of a class, so make sure it's available in your code.
  2. Create a Reflection Object: Create an object of the ReflectionClass, passing the class you want to inspect as a parameter.
  3. Get the Attributes: Use the getAttributes() method on the Reflection object to retrieve the attributes of the class.
  4. Work with the Attributes: You can then loop through the attributes and access their details.

Building on top of our earlier example, let's read the attributes assigned to the MyClass class. Here's how you can read the MySpecialLabel attribute from MyClass:

// Create a Reflection object for the class
$reflectionClass = new ReflectionClass('MyClass');
// Get the attributes of the class
$attributes = $reflectionClass->getAttributes();
// Loop through the attributes and print their names
foreach ($attributes as $attribute) {
  echo 'Found attribute: ' . $attribute->getName() . "\n";

This code will print:

Found attribute: MySpecialLabel

Key Takeaways

  • Attributes are structured, machine-readable metadata added to classes, methods, properties, functions, and parameters.
  • Created as classes marked with #[Attribute].
  • The Reflection API can be used to read attributes, allowing developers to introspect and analyze code at runtime.
  • Attributes can be specified to target specific elements like classes, methods, properties, etc.
  • Used for various purposes, including framework configuration, event listener registration, validation, serialization, and more.
  • They provide a cleaner, more expressive way to embed custom metadata, enhancing code organization and efficiency.
  • Often used in advanced development such as library and framework creation.
  • They were introduced in PHP 8.0, representing a modern feature of the language.


Please read this before commenting