<?php
define('REWIND', 'doing rewind' . PHP_EOL);
define('NEXT', 'doing next' . PHP_EOL);
define('CURRENT', 'show current: ');
define('VALID', 'is valid: ');
$func = function() {
echo '+ start' . PHP_EOL;
$array = range(0, 2);
foreach($array as $int) {
echo '+ before yield' . PHP_EOL;
yield $int;
echo '+ after yield' . PHP_EOL;
}
echo '+ end' . PHP_EOL;
};
$generator = $func();
// processing like in a loop
echo REWIND;
$generator->rewind();
echo VALID;
echo '"' . $generator->valid() . '"' . PHP_EOL;
echo CURRENT;
echo '"' . $generator->current() . '"' . PHP_EOL;
echo NEXT;
$generator->next();
echo VALID;
echo '"' . $generator->valid() . '"' . PHP_EOL;
echo CURRENT;
echo '"' . $generator->current() . '"' . PHP_EOL;
echo NEXT;
$generator->next();
echo VALID;
echo '"' . $generator->valid() . '"' . PHP_EOL;
echo CURRENT;
echo '"' . $generator->current() . '"' . PHP_EOL;
echo NEXT;
$generator->next();
echo VALID;
echo '"' . $generator->valid() . '"' . PHP_EOL;