There is however the 'yield from' statement.
function numbers() {
yield from [1,2,3];
yield from [4,5,6];
};
foreach (numbers() as $k => $v) { echo "$k => $v\n"; }
0 => 1
1 => 2
2 => 3
0 => 4
1 => 5
2 => 6
$a = 5;
$b = fn ($c) => $a * $b;
print($b(2)); // 10
print($b(4)); // 20
It's literally in the interface.
> Interface for external iterators or objects that can be iterated themselves internally
Note “external iterators or objects”. The Iterator interface is not exactly everyday PHP, it’s a specialist utility for making classes iterable so they can be accessed like arrays. Most developers will rarely use it directly, and it’s not being used in the parent comment’s example either.
Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration.
``` foreach (new LimitIterator($generator, 0, 3) as $value) { echo $value; } ```
No you don't. Other languages don't require it. There is no issue to get a position outside of iterator and it's more generic approach.
> Let me guess.
This just proves my point, you call this a “hot mess” whilst completely misunderstanding what it’s even used for.
The Iterator interface isn’t even used in the comment you first replied to.