Return
Simply gives back a unique value to the caller.
Yield
Transform the current function/method to return a Generator
, which will produce more than a unique value: every times yield
is triggered, it gives the value to the caller, one at a time, traditionally using a foreach
loop.
Yield
+ Return
Generators, in addition to generate values, can also, provide a unique returned value. That value won't be part of the looping around the generator, it must be accessed using Generator::getReturn()
method.
Return
+ Yield
This could be seen as a bug, however, it is not.
They are two phases:
- From code to bytecode: during this phase, the
generate()
function is seen to contain the yield
keyword, it is therefore marked as producing a Generator
.
- Execution: Because the
return
happens to be before the yield
, the generator doesn't have the chance to produce any value. However, the [1, 2, 3]
array can be retrieved with Generator::getReturn()
.
A complete annotated example:
// Generate integers 1 and 2
function generateIntegers1And2(): Generator {
yield 1; // <--+ <--+ <--+
yield 2; // <-+ <-+ <-+
} // | | |
// | | |
foreach (generateIntegers1And2() as $value) { // | | |
var_dump($value); // Shows 1, then 2 ->* | |
} // | |
// | |
function generateOuterYield(): Generator { // | |
// Yields the generator *itself* returned by | |
// generateIntegers1And2() not the actual values | |
// generated by it. | |
// This means we are producing here a generator | |
// of generator of integers. | |
yield generateIntegers1And2(); // <-+ | |
} // | | |
// | | |
foreach (generateOuterYield() as $value) { // | | |
var_dump($value); // ->* | |
// The two levels of imbrication means we have | |
// to loop once more to actually consume | |
// generateIntegers1And2 | |
foreach ($value as $val) { // | |
var_dump($val); // Shows 1, then 2 ->* |
} // |
} // |
// |
// A generator can just be returned as-is: |
function generateOuterReturn(): Generator { // |
return generateIntegers1And2(); // |
} // |
// |
// it doesn't change the way it is consumed |
foreach (generateOuterReturn() as $value) { // |
var_dump($value); // Shows 1, then 2 |
} // |
// |
function generateOuterYieldFrom(): Generator { // |
// First yield values generated by generateIntegers1And2() |
yield from generateIntegers1And2(); // *<---+
// then yield integers 3 |
yield 3; // <--+
// and 4 |
yield 4; // <-+
} // |
// |
foreach (generateOuterYieldFrom() as $value) { // |
var_dump($value); // Shows 1, 2, 3 and 4 ->*
}
function generateIntegers56AndReturn(): Generator {
yield 5; // <---+
yield 6; // <--+
// |
return ["five", "six"]; // <--+ |
} // | |
// | |
$gen = generateIntegers56AndReturn(); // | |
// | |
// Consume the values **yielded** by | |
// generateIntegers56AndReturn() | |
foreach ($gen as $value) { // | |
var_dump($value); // Shows 5, then 6 | ->*
} // |
// |
// Access the value **returned** by the generator |
var_dump($gen->getReturn()); // ->*
function wtf(): Generator {
return ["W", "T", "F", "!"];
// Without the following line, PHP would complain with a TypeError:
// Return value of wtf() must be an instance of Generator, array returned.
// The presence of a yield keyword anywhere inside the function makes it a Generator.
// However, since we return *before* reaching any *yield*, 42 is never yielded.
// This is empty generator!
yield 42;
}
$gen = wtf();
// This foreach loop is not entered!
foreach ($gen as $value) {
var_dump($value);
}
// However, we can loop on the array *returned* by wtf():
foreach ($gen->getReturn() as $value) {
echo $value; // Will print: WTF!
}
var_dump(generate()->GetReturn());
– Dimorph