Cannot rewind a generator that was already run

Description

In this code, $g includes the generator, and it is run twice, from the first loop in $h, and then, in the second loop with $i.

Example

<?php

$g = function (): Generator {
    yield from [1,2,3,4];
};    echo $i +;

$g = $g();

foreach($g as $h) {
    print h:$h -;
    foreach($g as $i) {
        echo i:$i +;

        break 1;
    }
}

?>

Solutions

  • Do not reuse the generator in two loops, but rather call $g() each time. It generates distinct and new data sources.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.