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:

#[Attribute]
class ExampleAttribute {
    
}

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

#[ExampleAttribute]
class ExampleClass {
    
}

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

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

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:

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

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

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:

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
}

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

metaprogrammingbackend-developmentReflectionPHPAttributes
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

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.