Re: New reflection methods for working with attributes

From: Date: Tue, 25 Jul 2023 20:21:01 +0000
Subject: Re: New reflection methods for working with attributes
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-120864@lists.php.net to get a copy of this message
On Tue, Jul 25, 2023, at 6:09 PM, Ollie Read wrote: > Hello all, > > A while back, I wrote a lengthy post about suggested improvements for > reflection, but I would like to come back to address additional methods > for dealing with attributes. (I have an open git issue here: > https://github.com/php/php-src/issues/8489) > > I'd like to introduce the following three methods on all reflect > classes that have attributes: > > hasAttribute(string $name, int $flags = 0): bool > > Uses the same filtering as getAttributes(), but returns true if a match > is found, false otherwise. > > getAttribute(string $name, int $flags = 0): ?ReflectionAttribute > > Also uses the same filtering as getAttributes(), except that it will > return an instance of ReflectionAttribute if one is found, null if none > are found, and will throw an exception of more than 1 is found. > > getNumberOfAttributes(?string $name = null, int $flags = 0): int > > Again, uses the same filtering as getAttributes(), and is to attributes > what getNumberOfParameters() is to parameters. I appreciate that this > methods use may not be obvious, but I wanted to include it for > consistency. > > I'm relatively confident that I have worked out the best method of > implementing these, and contributors have agreed to merge a PR, which I > will start shortly, should there be no objections. > > --- > Best Regards, > *Ollie Read* I'm not opposed to these, but would this be a good time to also add an interface for attributable reflection objects, so that we can type against that? These methods would all then go on that interface. --Larry Garfield

« previous php.internals (#120864) next »