Simple question.
When or how, by PHP or yourself do generators destroy their stacks?
Take the following example:
function doWork(): Generator
{
// create some objects.
$o1 = new stdClass();
$o2 = new stdClass();
// pause here and wait for data.
$value = yield 1;
// By referencing the above objects, they shouldn't destruct.
$o1->property = $value;
$o2->property = $value;
yield $o1;
yield $o2;
// End of stack.
}
// Create the generator.
$generator = doWork();
$value = $generator->current(); // $value will equal 1.
if ($x) {
$generator->send('Hello, World!'); // Continue execution of the generator.
$o1 = $generator->current();
$generator->next();
$o2 = $generator->current();
$generator->next(); // Complete the generator
var_dump($o1);
var_dump($o2);
} else {
// Do nothing with the generator.
}
// Carry on with script ...
In this example a generator is started and two objects are created. At this point it is yielded, and further data is requested.
An IF statement is reached.
Case 1
If $x
is true, the value "Hello, World!"
will be sent to the generator and the objects will be populated with their new properties.
The next time the yielded data is read, the objects will be returned.
Case 2
If $x
is false, the generator will no longer be used.
Question
In case 1, I would expect the stack to close like any other function, but what happens to the generator in case 2? Does it and all of the remaining object references stay in memory until the script ends?
Or does the loss of reference to $generator
cause it, and all references inside to be cleared out?