Get The Generator¶
<?php
function foo() {
yield 1;
return "a";
}
function goo($a) {
print $a . PHP_EOL;
}
goo(...($generator = foo()));
print $generator->getReturn();
// la
foreach (($generator = foo()) as $b) {
print $b . PHP_EOL;
}
print $generator->getReturn();
// la
?>
It is possible to catch a generator when it is called, by putting it in a variable. Once the generator has been used, it is possible to call a method such as getReturn on it.
This works with ... and foreach.
See Also¶
PHP Features¶
Last updated: 14 July 2026