Re: [RFC] [Discussion] Add ReflectionAttribute::getCurrent()
| From: | Benjamin Außenhofer | Date: | Tue, 12 May 2026 20:37:47 +0000 |
| Subject: | Re: [RFC] [Discussion] Add ReflectionAttribute::getCurrent() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130858@lists.php.net to get a copy of this message | ||
Am 04.05.2026, 22:24:39 schrieb Daniel Scherzer <daniel.e.scherzer@gmail.com
>:
> Hi internals,
>
> I'd like to start the discussion for a new RFC about adding a new method,
> ReflectionAttribute::getCurrent(), to access the current reflection target
> of an attribute.
>
> * RFC: https://wiki.php.net/rfc/reflectionattribute-getcurrent
> * Implementation: https://github.com/php/php-src/pull/21440
>
> Thanks,
> -Daniel
>
Hi Daniel,
I am not convinced this is needed. At every call site of
$reflector->getAttributes() you could inject the reflector back into the
attributes.
$attributes = $reflector->getAttributes();
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$instance->setReflector($reflector);
}
If you have a framework, or library, handling attributes, you could
introduce an interface: ReflectionAwareAttribute with setReflector() to
generalize this:
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof ReflectionAwareAttribute) {
$instance->setReflector($reflector);
}
}
The „magic" of this method knowing its context reminds me of internal
functions that were en voque in PHP 3/4 times, but for example
get.class(null) is not allowed anymore, func_get_args() is now superseded
by …$args and so on.
IFF we decide this is helpful, could we think about doing this with
interface injection like the code above or AttributeUtils?
greetings
Benjamin