Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generator with subgenerator
$generatorWithTwoWayCommunication = function()
$randomValue = mt_rand();
yield $randomValue;
$receivedFromOuterSpace = yield;
$returnValue = yield from subGenerator($receivedFromOuterSpace, $randomValue);
return $returnValue;
function subGenerator($valueFromOuterSpace, $randomValue)
yield $valueFromOuterSpace * $valueFromOuterSpace;
yield $valueFromOuterSpace * $randomValue;
return 'Lis Witalis';
$generator = $generatorWithTwoWayCommunication();
$valueFromFirstIteration = $generator->current();
$valueFromSecondIteration = $generator->send(666);
$valueFromThirdIteration = $generator->current();
$valueReturnedFromGenerator = $generator->getReturn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment