# New reflection methods for working with attributes

- Thread: https://externals.io/message/120863
- Original archive: https://news-web.php.net/php.internals/120863
- Started by Ollie Read on 2023-07-25T18:09:17+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #120863 — Ollie Read — 2023-07-25T18:09:17+00:00

URL: https://externals.io/message/120863#120863

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*

---

## #120864 — Larry Garfield — 2023-07-25T20:21:01+00:00

URL: https://externals.io/message/120863#120864 · In reply to: #120863

> 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

---

## #120865 — David Gebler — 2023-07-25T20:24:21+00:00

URL: https://externals.io/message/120863#120865 · In reply to: #120863

What's the difference between this and what was proposed in
https://externals.io/message/120799 ? I don't get why this wouldn't require
an RFC.

---

## #120866 — Robin Chalas — 2023-07-25T20:55:00+00:00

URL: https://externals.io/message/120863#120866 · In reply to: #120865

> What's the difference between this and what was proposed in
https://externals.io/message/120799 ? I don't get why this wouldn't require
an RFC.

FY  I started working on the implementation., hopefully it will bring more arguments in favor of that RFC which I’m willing to submit asap as well.

— Robin Chalas

Robin Chalas
Principal Engineer @Les-Tilleuls.coop
03 66 72 43 94
robin@les-tilleuls.coop
82 Rue Winston Churchill - 59160 Lomme
Le 25 juil. 2023 à 22:24 +0200, David Gebler <davidgebler@gmail.com>, a écrit :
> What's the difference between this and what was proposed in
> https://externals.io/message/120799 ? I don't get why this wouldn't require
> an RFC.

---

## #120867 — Ollie Read — 2023-07-25T21:33:20+00:00

URL: https://externals.io/message/120863#120867 · In reply to: #120864

> 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.

I was going for a tiered approach. Once these methods were in, I would propose adding an Attributable or ReflectorWithAttributes interface, as well as a separate proposal to have ReflectionAttribute aware of the Attributable/ReflectionWithAttributes that it came from.

> What's the difference between this and what was proposed in
> https://externals.io/message/120799 ? 

My proposal adds 3 methods, not just one, and was a followup to something from a while ago.

> I don't get why this wouldn't require an RFC.

It's adding missing methods that should realistically be there. It breaks nothing, nor does it introduce entirely brand new things. It's the same as getParameter()/hasParameter() <https://github.com/php/php-src/pull/10431> and hasPrototype() <https://github.com/php/php-src/pull/8487>. I couldn't tell you exactly why, just that the core contributors said so.

> FY  I started working on the implementation., hopefully it will bring more arguments in favor of that RFC which I’m willing to submit asap as well.

Is it just the hasAttribute() method you're working on? I'm happy to help out if you need/want it. I was moments away from opening a PR.

---
Best Regards,
*Ollie Read*

---

## #120868 — G. P. B. — 2023-07-26T01:31:05+00:00

URL: https://externals.io/message/120863#120868 · In reply to: #120865

> What's the difference between this and what was proposed in
> https://externals.io/message/120799 ? I don't get why this wouldn't
> require
> an RFC.
>

Because it frankly doesn't require an RFC.
I think people are getting confused as to when one is required.

Adding methods for completeness that should just be there and no-one
complains can just get added.
The email is a courtesy to know if some people are going to bikeshed.
Now, adding an interface is more of a change that probably warrants one,
but even then it is fuzzy.

Best,

George P. Banyard

---

## #120869 — G. P. B. — 2023-07-26T01:32:58+00:00

URL: https://externals.io/message/120863#120869 · In reply to: #120866

> FY  I started working on the implementation., hopefully it will bring more
> arguments in favor of that RFC which I’m willing to submit asap as well.
>

For future reference, please just open a PR with the implementation before
writing an RFC for those kinds of things. As self-contained, small features
do not necessarily require them.

Best regards,

George P. Banyard

PS: Side note, please do not bottom post.

---

## #120870 — Robin Chalas — 2023-07-27T16:47:50+00:00

URL: https://externals.io/message/120863#120870 · In reply to: #120869

>For future reference, please just open a PR with the implementation before writing an RFC for those kinds of things. As self-contained, small features do not necessarily require them.
>
>Best regards,

>George P. Banyard
>
>PS: Side note, please do not bottom post

Alright, I will just ship the PR for `hasAttribute()` then.
Thanks for the hints and feedback.
And sorry for the bad posting style, I hope this one is ok.

-- Robin Chalas

---

## #120878 — Ollie Read — 2023-08-03T18:19:17+00:00

URL: https://externals.io/message/120863#120878 · In reply to: #120870

> Alright, I will just ship the PR for `hasAttribute()` then.
> Thanks for the hints and feedback.
> And sorry for the bad posting style, I hope this one is ok.

In that case, are you happy for me to PR the other two methods I suggested, or would you like to handle that?

---
Best Regards,
*Ollie Read*

---

## #120879 — Robin Chalas — 2023-08-03T18:40:38+00:00

URL: https://externals.io/message/120863#120879 · In reply to: #120878

> In that case, are you happy for me to PR the other two methods I suggested, or would you like to handle that?

Please go ahead! Thanks for caring.
And if you already have the patch ready for hasAttribute() too but just care enough to leave it to the wanna-be-first-time-contributor that I am, please consider sharing it to me so that I can compare with what I have once I’m back from vacation next week (and either come back to you with questions or incline myself :)). Or I’ll send you mine for early review when I’m back to keyboard if you’re ok.

Cheers

-- Robin Chalas
Le 3 août 2023 à 20:20 +0200, Ollie Read <php@ollie.codes>, a écrit :
>
> > […]
>
> In that case, are you happy for me to PR the other two methods I suggested, or would you like to handle that?
>
> ---
> Best Regards,
> *Ollie Read*

---

## #120889 — Ollie Read — 2023-08-07T11:03:03+00:00

URL: https://externals.io/message/120863#120889 · In reply to: #120879

> Please go ahead! Thanks for caring.
> And if you already have the patch ready for hasAttribute() too but just care enough to leave it to the wanna-be-first-time-contributor that I am, please consider sharing it to me so that I can compare with what I have once I’m back from vacation next week (and either come back to you with questions or incline myself :)). Or I’ll send you mine for early review when I’m back to keyboard if you’re ok.

I'll wait for your implementation, as all the methods will be virtually identical, so it's best to base it off your hasAttribute method.

---
Best Regards,
*Ollie Read*
