0
0
PHPprogramming~10 mins

Yield from delegation in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Yield from delegation
Start generator
Yield values from sub-generator
Receive value from caller
Pass value to sub-generator
Sub-generator yields value
Yield value to caller
Repeat until sub-generator ends
End generator
The main generator delegates yielding and receiving values to a sub-generator using 'yield from', passing values back and forth until the sub-generator finishes.
Execution Sample
PHP
<?php
function subGen() {
  yield 1;
  yield 2;
}
function mainGen() {
  yield from subGen();
}
foreach(mainGen() as $val) { echo $val; }
This code delegates yielding values 1 and 2 from subGen() through mainGen() to the caller.
Execution Table
StepActionGenerator StateValue YieldedCaller Receives
1mainGen() startsmainGen active, subGen not startednonenone
2yield from subGen() starts subGensubGen active at first yield11
3Caller receives 1subGen paused after yield 111
4Caller requests nextsubGen resumes22
5Caller receives 2subGen paused after yield 222
6Caller requests nextsubGen endsnonenone
7mainGen endsmainGen endsnonenone
💡 subGen finishes yielding all values, so mainGen ends as well
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
mainGen statenot startedactive, delegatingactive, delegatingendedended
subGen statenot startedactive at yield 1active at yield 2endedended
value yieldednone12nonenone
Key Moments - 3 Insights
Why does mainGen yield values even though it has no explicit yield statements?
Because 'yield from' delegates yielding to subGen, so mainGen yields whatever subGen yields (see execution_table steps 2 and 4).
What happens when the sub-generator finishes?
When subGen ends (step 6), mainGen also ends immediately (step 7), stopping the iteration.
How does the caller receive values from subGen through mainGen?
The caller calls mainGen, which delegates to subGen via 'yield from', passing yielded values directly to the caller (steps 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does the caller receive at step 3?
A1
B2
Cnone
DsubGen object
💡 Hint
Check the 'Value Yielded' and 'Caller Receives' columns at step 3.
At which step does subGen finish yielding all values?
AStep 4
BStep 6
CStep 7
DStep 2
💡 Hint
Look for 'subGen ends' in the 'Generator State' column.
If we add a third yield in subGen, how would the execution_table change?
AmainGen would end earlier.
BCaller would receive no values.
CThere would be an extra step with value 3 yielded before subGen ends.
DsubGen would never end.
💡 Hint
Think about how 'yield from' passes all yields from subGen to caller.
Concept Snapshot
PHP 'yield from' lets one generator pass values from another generator directly.
Syntax: yield from subGenerator();
It forwards all yields and receives transparently.
Useful to simplify generator delegation.
Stops when sub-generator ends.
Full Transcript
This example shows how PHP's 'yield from' delegates yielding values from a sub-generator to the main generator. The main generator starts, then calls 'yield from' on the sub-generator. The sub-generator yields values 1 and 2, which are passed directly to the caller. When the sub-generator finishes, the main generator ends too. This delegation allows the main generator to yield all values from the sub-generator without writing explicit loops. The execution table traces each step, showing generator states and values passed. The variable tracker shows how generator states and yielded values change over time. Key moments clarify common confusions about delegation and generator ending. The quiz tests understanding of yielded values and generator lifecycle.