Proposal to improve consistency of is_iterable() and isIterable Reflection class method with what foreach actually expects #10554
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What I did?
Basically I have
json_decode
object, which contains key value configuration (which comes from API), like thisand I want to iterate over those key values with foreach:
which works fine. However, I want to guard this foreach from iterating on improper values. That's why I used
is_iterable()
(https://www.php.net/manual/en/function.is-iterable.php), In PHP RFC: Iterable (https://wiki.php.net/rfc/iterable) we can read:What I wanted to happen?
I wanted that
is_iterable
would guard my foreach from every wrong value, likefalse
,null
and would allow object like above. See example:What actually happened?
It turned that above object passed to
is_iterable()
returns false, which suprise me because it can be perfectlly fined passed to foreach, which has this warning if you pass something expected:My setup
This happens on PHP 7.3, 8.1 and 8.2 (those that I tested on). I'm using Ubuntu 20.04
Summary
My PR propose to fix this inconsistency (or at least in my opinion it's inconsistency).
Please take a look and let me know what do you think, thanks!
Here is bug reported that I made: php/doc-en#2378
Best regards,
stalkonr