Add Reflection*::getAttribute(): ?ReflectionAttribute method#22092
Add Reflection*::getAttribute(): ?ReflectionAttribute method#22092vudaltsov wants to merge 1 commit into
Conversation
What about https://www.php.net/manual/en/function.array-first.php? |
|
Yes, I use that function in 8.5, but it seems that I get the first attribute in 99% of the cases, so it makes sense to add the method anyway. |
|
I don't particularly like the dedicated method: If there is more than one attribute it silently “loses data”. If the “first (and only) attribute only” is the desired semantic then doing that in userland has become trivial with |
|
The thing is, 99% of the time you want to pass Maybe the method should be called |
That would be explicit, but would not be meaningfully simpler than just using I believe good language design is having multiple simple features that compose well, not having an explicit function for everything, since the former makes the language much easier to learn and more flexible with regard to new use cases. Given the disagreement here, I'm also marking the PR as “Requires RFC”. |
|
I actually tend to agree with you. I will think about this for a day and then either write to internals or just close the PR. Thank you for a quick feedback! |
Currently, getting a single attribute requires either
getAttributes()[0] ?? nullor a temporary variable. This is the most common case since attributes are non-repeatable by default.I propose to add
getAttribute()to all reflection classes that have getAttributes(), with the same signature and filtering behavior asgetAttributes().There is some code duplication between
read_attribute()andread_attributes()— I'm not sure it's worth eliminating, but happy to refactor if needed.Also: does this need an RFC? I've seen small non-breaking changes like this are sometimes merged without an RFC.