Re: Validating #[\Attribute] targets
| From: | Claude Pache | Date: | Sat, 26 Jul 2025 14:37:02 +0000 |
| Subject: | Re: Validating #[\Attribute] targets | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128240@lists.php.net to get a copy of this message | ||
> Le 25 juil. 2025 à 18:37, Daniel Scherzer <daniel.e.scherzer@gmail.com> a écrit :
>
> Hi internals,
>
> The #[\Attribute] attribute is used to mark classes as being usable as attributes. It currently
> does not trigger any error if added to a trait, interface, enum, or abstract class. However, if any
> of those *were* used as an attribute, calling ReflectionAttribute::newInstance() would fail; 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.
>
> I have proposed https://github.com/php/php-src/pull/19154 to
> add validation to the #[Attribute] attribute, and it was suggested that I post here on the mailing
> list in case there were any objections. So, are there?
>
> -Daniel
Hi,
Validating more precisely the target of #[Atrribute] is reasonable. Now, observing that there are
several internal attributes that may benefit this capability (e.g. #[Attribute],
#[AllowDynamicProperties], #[Deprecated] on traits as currenty voted on), I wonder if we can also
expose this capability to user-defined attributes? Concretely, there could be the following
additional constants:
* Attribute::TARGET_INTERFACE
* Attribute::TARGET_ABSTRACT_CLASS
* Attribute::TARGET_TRAIT
* Attribute::TARGET_ENUM
* Attribute::TARGET_REGULAR_CLASS
and Attribute::TARGET_CLASS would be equal to the result of combining all of those with the
| operator.
—Claude
—Claude