RE: [PHP-DEV] Validating #[\Attribute] targets
| From: | Juris Evertovskis | Date: | Sat, 26 Jul 2025 11:29:30 +0000 |
| Subject: | RE: [PHP-DEV] Validating #[\Attribute] targets | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128236@lists.php.net to get a copy of this message | ||
Hey,
> if you are not going to call ReflectionAttribute::newInstance(), then you don't
> really need to mark the trait/interface/enum/abstract class as an attribute.
Personally I never knew the
#[\Attribute] is only required to do
->newInstance().
The docs make it seem like it’s a boilerplate to have an attribute at all: https://www.php.net/manual/en/language.attributes.overview.php
And the documented example does not call newInstance although the attribute is defined
with #[Attribute].
I’m also confused as the example created by Ilija https://3v4l.org/8Lih3#v8.4.10 that would supposedly break
does not include #[Attribute] at all. But it includes an enum being used as attribute
without it using the label itself. So what would actually break? Using enums as attributes or
declaring them as such?
Btw while reviewing the docs https://www.php.net/manual/en/language.attributes.classes.php
I noticed that
> It is recommended to define a separate class for each attribute.
Recommended as opposed to what other options? I don’t think I can use a random string as an
attribute without defining it, right? So what is this recommendation steering me away from?
BR,
Juris