Iterating through a list of items is among the most common things in any programming language. PHP makes it easy to iterate over a diverse collection of data using the foreach construct. Many languages differentiate various data types of collection data, such as dictionary, list, set, tuple, and alike. PHP, however, does not dwell that much on data structures and simply uses the array() or [] constructs most of the time for its collections. This, in turn, can have a negative impact on creating large arrays in memory, which could cause exceeding memory limits or even increased processing times.
Generators allow us to write code that uses foreach to iterate over a set of data without needing to build an array. They are like a function that yields as many values as needed, instead of returning just one, which gives them an iterator-like behavior. While generators have been around from PHP 5.5, they lacked more advanced functionality. Generator delegation is one of the improvements made available with the release of PHP 7.
Let's take a look at the following example:
function even() {
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
yield $i;
}
}
}
function odd() {
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 != 0) {
yield $i;
}
}
}
function mix() {
yield -1;
yield from odd();
yield 17;
yield from even();
yield 33;
}
// 2 4 6 8 1 0
foreach (even() as $even) {
echo $even;
}
// 1 3 5 7 9
foreach (odd() as $odd) {
echo $odd;
}
// -1 1 3 5 7 9 17 2 4 6 8 10 33
foreach (mix() as $mix) {
echo $mix;
}
Here, we define three generator functions: even, odd, and mix. The mix function demonstrates the concept of generator delegation via the use of yield from <expr>. Whereas, <expr> is any expression that evaluates to a traversable object or array. We can see that the result of looping through the mix function echoes all of the yielded values from both itself as well as the even and odd functions.
The generator delegation syntax allows the factoring of yield statements into smaller conceptual units, giving generators the similar organizational functionality as methods give to classes. Used carefully, this can improve our code quality and readability.