A generator function looks like a normal function, except that the generator produces multiple values on demand instead of returning a value.
When the generator function is called, it returns an object that can be iterated. When you iterate over that object (for example, through a foreach loop), PHP invokes the generator function whenever a value is needed, and then saves the generator state when the generator outputs a value so that the next value can be recovered when needed.
If there are no more values to output, the generator function can simply exit, and the calling code will continue as if an array had exhausted all the values.
Note:
The generator cannot return a value: doing so will result in a compilation error. The empty return statement in the generator is a valid syntax and interrupts the generator.
Yield keyword
The core of the generator function is the yield keyword. In the simplest form, a yield statement looks very much like a return statement, except that yield provides a value to view the generator's code and then pauses execution of the generator instead of returning a value.
Example Simple example of #1 output value
<?php function Gen_one_to_three () {for ($i = 1; $i <= 3; $i + +) { //Note that $i is preserved between y Ields. Yield $i; } } $generator = Gen_one_to_three (); foreach ($generator as $value) { echo "$value \ n"; }? >
The above routines will output:
123
Note:
Internally, the serialized integer key will be paired with an output value, just like a non-associative array.
Caution If you use yield in a context expression (for example, in an assignment statement to the right of the equals sign), be sure to enclose it in parentheses. For example, the following code is correct:
$data = (yield $value);
However, the following code will have a parse error:
$data = yield $value;
This syntax may be used with the Send () method in the Builder object.
Use key to get the value
PHP supports associative arrays, as well as generators. In addition to generating simple values, as shown above, you can also generate keys at the same time.
The syntax for generating key/value pairs is very simple and can be used to define associative arrays, as shown below.
Example #2 yielding a key/value pair
<?php/ * The input is semi-colon separated fields, with the first * field being an ID to use as a key. */ $ input = <<< ' EOF ' 1; PHP; Likes dollar signs2; Python; Likes whitespace3; Ruby; Likes blockseof; function Input_parser ($input) { foreach (explode ("\ n", $input) as $line) { $fields = explode ('; ', $line); $id = Array_shift ($fields); Yield $id = = $fields; } } foreach (Input_parser ($input) as $id = + $fields) { echo "$id: \ n"; echo "$fields [0]\n]; echo "$fields [1]\n"; }? >
The above routines will output:
1: PHP likes dollar signs2: Python likes whitespace3: Ruby likes blocks
Caution, like the early generation of simple values, generates key/value in a context expression to enclose the yield statement in parentheses:
$data = (Yield $key = $value);
Generate a Null value
Yield can be used without parameters to generate null values using auto-generated keys.
Example #3 Yielding NULLs
<?php function Gen_three_nulls () { foreach (range (1, 3) as $i) { yield; } } Var_dump (Iterator_to_array (Gen_three_nulls ()));? >
The above routines will output:
Array (3) { [0]=>null [ 1]=>null [2]=>null}]
Generating values by reference
A generator function can produce a value by reference. This is the same as in returning references from functions: implemented by adding & to the function name.
Example #4 yielding values by reference
<?php function &gen_reference () { $value = 3; while ($value > 0) { yield $value; } } /* Note that we can change it in the loop . * The generator is yielding references, $value * within gen_reference () changes. */ foreach (Gen_reference () as & $number) { echo (--$number). ' ... '; }? >
The above routines will output:
2 ... 1 ... 0 ...