PHP's iterator_*()
family currently only accept \Traversable
s (i.e. they reject plain array
s). This is unnecessarily limiting.
Specifically this concerns the iterator_to_array()
and iterator_count()
functions. While each of them has an array-specific counterpart, the fact that one needs to choose either the array-specific variant or the everything-but-array variant makes writing code the deals with arbitrary iterable
s unnecessarily verbose.
As an example: Allowing iterator_to_array()
to take an array, makes it much easier to write functions accepting an iterable
and processing it using array_map()
et al:
function before(iterable $foo) { if (!is_array($foo)) { $foo = iterator_to_array($foo); } return array_map(strlen(...), $foo); } function after(iterable $foo) { $foo = iterator_to_array($foo); return array_map(strlen(...), $foo); }
The $iterator
parameter of iterator_to_array()
and iterator_count()
should be widened from \Traversable
to iterable
(i.e. to \Traversable|array
).
Specifically if this RFC is accepted the following shall hold:
iterator_to_array($array, true) == $array iterator_to_array($array, false) == array_values($array)
iterator_count($array) == count($array)
This function is not part of this proposal, because it is non-obvious how to define the behavior for array
s, given that it does not pass the Iterator
to the callback by default.
None, this is a purely type widening change.
next PHP 8.x
none
none
none
none
none
none
Anything that isn't iterator_to_array()
or iterator_count()
.
none
Each vote requires a 2/3 majority.
Voting opened 2022-07-05 14:30 UTC and closes on 2022-07-19 14:45 UTC.
iterable_*
prefix: https://wiki.php.net/rfc/iterable_to_array-and-iterable_countiterable_to_array()
: https://stackoverflow.com/q/44587973/782822none