My current use case is I have a Generator
that takes an iterable and uses yield from
to return everything from it (similar to the example in the PHP docs). I have two examples below.
In this example, I am manually iterating through the Generator using Iterator methods: rewind()
, valid()
, current()
, and next()
. This example leads into an infinite loop and I never reach the var_dump
call.
<?php
$arr = [10, 20, 30, 40, 50];
$itr = new ArrayIterator($arr);
function q() {
global $itr;
yield from $itr;
}
$a = [];
q()->rewind();
while (q()->valid()) {
$a[q()->key()] = q()->current();
q()->next();
}
var_dump($a); // I never reach this
In this example, I have a normal call to iterator_to_array()
and it successfully completes.
<?php
$arr = [10, 20, 30, 40, 50];
$itr = new ArrayIterator($arr);
function q() {
global $itr;
yield from $itr;
}
$a = iterator_to_array(q());
var_dump($a);
I found a similar question but it's not quite the answer I'm looking for since that question ends up using iterator_to_array()
. Similarly, the PHP docs advise using iterator_to_array
with the use_keys
parameter. But I'd like to make the while loop work if that's possible.
What am I doing wrong in the first example that is causing me to enter an infinite loop?