Custom attribute classes declare legal targets and constructor arguments for structured metadata.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Attributes: Writing Custom Attribute Classes Works
Custom attribute classes declare legal targets and constructor arguments for structured metadata.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
#[Attribute(Attribute::TARGET_CLASS)] class ResourceName { public function __construct(public string $value) {} }
#[ResourceName("inventory")] class InventoryResource {}
echo (new ReflectionClass(InventoryResource::class))->getAttributes(ResourceName::class)[0]->newInstance()->value;
Output:
inventory
Custom attribute classes declare legal targets and constructor arguments for structured metadata.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
#[Attribute(Attribute::TARGET_PARAMETER)] class TrimInput {}
class ProfileUpdater { public function rename(#[TrimInput] string $name): void {} }
$parameter = (new ReflectionMethod(ProfileUpdater::class, "rename"))->getParameters()[0];
var_dump($parameter->getAttributes(TrimInput::class) !== []);
Output:
bool(true)
Limit attribute targets with Attribute::TARGET_* and validate its constructor inputs.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
#[Attribute(Attribute::TARGET_PROPERTY)] class MaxLength { public function __construct(public int $characters) {} }
class CustomerProfile { #[MaxLength(40)] public string $name = "Alex"; }
$property = new ReflectionProperty(CustomerProfile::class, "name");
echo $property->getAttributes(MaxLength::class)[0]->newInstance()->characters;
Output:
40
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Limit attribute targets with Attribute::TARGET_* and validate its constructor inputs.
- Custom attribute classes declare legal targets and constructor arguments for structured metadata.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Attributes: Writing Custom Attribute Classes | Custom attribute classes declare legal targets and constructor arguments for structured metadata. |
| Alternative approach | Hard-coded conventions work for small projects; custom attributes centralize repeated metadata. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Attributes: Writing Custom Attribute Classes | Limit attribute targets with Attribute::TARGET_* and validate its constructor inputs. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Hard-coded conventions work for small projects; custom attributes centralize repeated metadata. |
Key Takeaways
- Custom attribute classes declare legal targets and constructor arguments for structured metadata.
- Limit attribute targets with Attribute::TARGET_* and validate its constructor inputs.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Attributes: Writing Custom Attribute Classes is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.