ReflectionAttribute describes metadata attached to a declaration and can create an attribute instance when requested.
What Is ReflectionAttribute::isRepeated in PHP?
ReflectionAttribute::isRepeated() reads the declared attribute metadata.
ReflectionAttribute::isRepeated() : bool
This operation does not accept method arguments.
Return type: bool. getArguments() reads constructor arguments without instantiating the attribute; newInstance() constructs its PHP object and may throw on invalid arguments.
Example 1: Understanding the Basic Behavior
<?php
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Label { public function __construct(public string $name) {} }
#[Label("new")]
class Article {}
$attribute = (new ReflectionClass(Article::class))->getAttributes(Label::class)[0];
var_dump($attribute->isRepeated());
Output:
bool(false)
Explanation: ReflectionAttribute::isRepeated() reads the declared attribute metadata.
Example 2: Testing Another Case
<?php
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Label { public function __construct(public string $name) {} }
#[Label("draft")]
#[Label("urgent")]
class Article {}
$attribute = (new ReflectionClass(Article::class))->getAttributes(Label::class)[0];
var_dump($attribute->isRepeated());
Output:
bool(true)
Explanation: getArguments() reads constructor arguments without instantiating the attribute; newInstance() constructs its PHP object and may throw on invalid arguments.
Practical Example: Applying the Operation
<?php
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Label { public function __construct(public string $name) {} }
#[Label("published")]
class Article {}
$attribute = (new ReflectionClass(Article::class))->getAttributes(Label::class)[0];
var_dump($attribute->isRepeated());
Output:
bool(false)
Explanation: Reading constructor arguments is different from constructing an attribute; newInstance() may execute constructor validation.
Important Behavior and Edge Cases
- getArguments() reads constructor arguments without instantiating the attribute; newInstance() constructs its PHP object and may throw on invalid arguments.
- Reading constructor arguments is different from constructing an attribute; newInstance() may execute constructor validation.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionAttribute::isRepeated | ReflectionAttribute::isRepeated() reads the declared attribute metadata. |
ReflectionClass::getAttributes() | ReflectionClass::getAttributes() finds descriptors; ReflectionAttribute methods inspect a selected descriptor. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming an attribute descriptor executes the annotated application behavior automatically. | getArguments() reads constructor arguments without instantiating the attribute; newInstance() constructs its PHP object and may throw on invalid arguments. |
| Choosing ReflectionClass::getAttributes() without checking whether it provides the same result. | ReflectionClass::getAttributes() finds descriptors; ReflectionAttribute methods inspect a selected descriptor. |
Key Takeaways
- ReflectionAttribute::isRepeated() reads the declared attribute metadata.
- getArguments() reads constructor arguments without instantiating the attribute; newInstance() constructs its PHP object and may throw on invalid arguments.
- Reading constructor arguments is different from constructing an attribute; newInstance() may execute constructor validation.
The examples demonstrate where ReflectionAttribute::isRepeated fits within attribute reflection. Verify the relevant preconditions whenever you adapt the code to real data.