Backend Development 4 min read

Using PHP 8 Attributes to Extend Classes: Syntax, Examples, and Reflection

This article explains PHP 8’s new Attributes feature, showing how to define attribute classes, apply them to classes, methods, and properties, and retrieve their values using the Reflection API, with complete code examples.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP 8 Attributes to Extend Classes: Syntax, Examples, and Reflection

With the release of PHP 8, the new language feature Attributes (属性) is introduced, allowing developers to add metadata to code elements such as classes, methods, and properties.

This article provides a detailed guide on using Attributes to extend classes in PHP 8, including concrete code examples.

First, the basic syntax of Attributes is explained. In PHP 8, an attribute class is defined using #[Attribute] . For example:

<code>#[Attribute]
class ExampleAttribute {
    
}
</code>

After defining an attribute, it can be applied to a class, method, or property. For instance:

<code>#[ExampleAttribute]
class ExampleClass {
    
}
</code>

Attributes can also accept parameters to configure them more flexibly. An example with a constructor argument and a target restriction:

<code>#[Attribute(Attribute::TARGET_CLASS)]
class ExampleAttribute {
    public function __construct(private string $value) {
        
    }
}
</code>

The article then shows how to retrieve and use attribute values via the Reflection API. By creating a ReflectionClass for ExampleClass and calling getAttributes() , the attached ExampleAttribute instances can be instantiated and their values accessed:

<code>$classReflector = new ReflectionClass(ExampleClass::class);
$attributes = $classReflector->getAttributes(ExampleAttribute::class);

foreach ($attributes as $attribute) {
    $instance = $attribute->newInstance();
    echo $instance->value; // outputs "example value"
}
</code>

Attributes can also be used on methods to enforce access control or add functionality. The following example adds ExampleAttribute to a method and retrieves it using ReflectionMethod :

<code>class ExampleClass {
    #[ExampleAttribute]
    public function exampleMethod() {
        // method body
    }
}

$methodReflector = new ReflectionMethod(ExampleClass::class, 'exampleMethod');
$attributes = $methodReflector->getAttributes(ExampleAttribute::class);

foreach ($attributes as $attribute) {
    // process method attribute
}
</code>

Through these examples, readers gain a clear understanding of how Attributes can be used to add metadata, improve code readability, and meet various business requirements in PHP 8.

The article concludes by encouraging developers to define and use their own Attributes to enhance code maintainability and flexibility.

MetaprogrammingBackend DevelopmentReflectionphpattributesPHP8
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.