Yield from delegation helps you easily get values from one generator inside another without extra loops.
Yield from delegation in PHP
yield from <generator_expression>;This passes all yielded values from the inner generator to the outer one.
It simplifies code by avoiding manual loops to yield each value.
yield from passes values from gen1 inside gen2.<?php function gen1() { yield 1; yield 2; } function gen2() { yield from gen1(); yield 3; } foreach (gen2() as $value) { echo $value . " "; }
yield from to produce letters then numbers.<?php function letters() { yield 'a'; yield 'b'; } function numbers() { yield 1; yield 2; } function combined() { yield from letters(); yield from numbers(); } foreach (combined() as $item) { echo $item . " "; }
This program shows how yield from lets outerGenerator include all values from innerGenerator easily.
<?php function innerGenerator() { yield 'apple'; yield 'banana'; } function outerGenerator() { yield 'start'; yield from innerGenerator(); yield 'end'; } foreach (outerGenerator() as $fruit) { echo $fruit . "\n"; }
yield from can delegate to any iterable, not just generators.
It helps keep generator code clean and readable.
Yield from delegation passes values from one generator to another easily.
It avoids writing loops to yield each value manually.
Use it to combine or chain generators simply.
Practice
What does the yield from statement do in PHP generators?
Solution
Step 1: Understand the purpose of
yield fromyield fromis used to delegate yielding values from another generator or iterable, so you don't have to write a loop to yield each value manually.Step 2: Compare with other options
Stopping a generator, converting it to an array, or throwing exceptions are unrelated toyield from.Final Answer:
It delegates yielding values from another generator or iterable. -> Option AQuick Check:
yield fromdelegates values [OK]
yield from passes values from another generator [OK]- Thinking
yield fromstops the generator - Confusing
yield fromwith array conversion - Assuming it throws exceptions
Which of the following is the correct syntax to delegate yielding from another generator $gen inside a generator function?
function gen() {
// your code here
}Solution
Step 1: Recall correct
The correct syntax to delegate yielding from another generator isyield fromsyntaxyield from $gen;.Step 2: Check other options for syntax errors
yield $gen;yields the generator object itself, not its values.yield($gen);is invalid syntax.yield to $gen;is not valid PHP syntax.Final Answer:
yield from $gen; -> Option DQuick Check:
Correct syntax usesyield from[OK]
yield from to delegate generators [OK]- Using just
yield $gen;instead ofyield from - Adding parentheses incorrectly
- Writing invalid keywords like 'yield to'
What will be the output of the following PHP code?
function gen1() {
yield 1;
yield 2;
}
function gen2() {
yield from gen1();
yield 3;
}
foreach (gen2() as $value) {
echo $value . ' ';
}Solution
Step 1: Understand
This delegates yielding values 1 and 2 fromyield from gen1()ingen2()gen1()insidegen2().Step 2: Trace the output of the foreach loop
The loop yields 1, then 2 fromgen1(), then yields 3 fromgen2(). So output is "1 2 3 "Final Answer:
1 2 3 -> Option CQuick Check:
Delegation yields 1,2 then 3 [OK]
yield from outputs all delegated values first [OK]- Assuming order is changed
- Forgetting the last yield after delegation
- Thinking
yield fromyields the generator object
Identify the error in this PHP generator code:
function genA() {
yield 1;
yield 2;
}
function genB() {
yield genA();
yield 3;
}
foreach (genB() as $val) {
echo $val . ' ';
}Solution
Step 1: Analyze
This yields the generator objectyield genA();ingenB()genA()itself, not the values it produces.Step 2: Understand correct delegation with
To yield values fromyield fromgenA(), useyield from genA();instead ofyield genA();.Final Answer:
It yields the generator object instead of its values. -> Option AQuick Check:
Useyield fromto delegate values [OK]
yield from to yield values, not the generator object [OK]- Using
yieldinstead ofyield fromfor delegation - Assuming yielding a generator yields its values
- Ignoring the need to delegate properly
Given two generators genX() yielding numbers 1 to 3 and genY() yielding numbers 4 to 6, how can you create a generator genZ() that yields all numbers from 1 to 6 using yield from?
function genX() {
yield 1;
yield 2;
yield 3;
}
function genY() {
yield 4;
yield 5;
yield 6;
}
function genZ() {
// Fill in code here
}Solution
Step 1: Use
To yield all numbers from 1 to 6 in sequence, delegateyield fromto delegate both generators in ordergenX()first, thengenY()usingyield from.Step 2: Check order and syntax
yield from genX(); yield from genY(); yields 1,2,3 then 4,5,6. yield from genY(); yield from genX(); reverses order. Options B and C yield generator objects or invalid expressions.Final Answer:
yield from genX(); yield from genY(); -> Option BQuick Check:
Delegate generators in order withyield from[OK]
yield from statements [OK]- Yielding generators instead of delegating
- Reversing the order of delegation
- Trying to add generators directly
Ready to prove it?
Pass the test and this pattern moves unknown
