A generator cannot return values: doing so produces a compilation error. However, the return null is a valid syntax and it will terminate the generator to continue execution.
Yield keywords
The yield keyword is the core of the generator function. Its simplest call form looks like a return statement. The difference is that normal return will return the value and terminate the function execution, yield returns a value to call the code of this generator cyclically and only suspends the execution of the generator function.
Note:
Internally, consecutive integer indexes are matched for the generated values, just like an unassociated array.
If you use yield in an expression context (for example, on the right of a value expression), you must use parentheses to enclose the yield declaration. For example, this is valid:
$data = (yield $value);
This is not legal, and a compilation error occurs in PHP5:
$data = yield $value;
The parenthetical restrictions do not apply in PHP 7.
This syntax can be used with the Generator: send () method of the Generator object.
Specify a key name to generate a value
PHP arrays support joined key-value pairs, and generators support the same. In addition to generating simple values, you can also specify the key name when generating values.
As shown below, generating a key-value pair is very similar to defining an associated array.
Example #2 generate a key-value pair
$ Fields ;}} foreach (input_parser ($ input) as $ id =>$ fields) {echo "$ id: \ n "; echo "$ fields [0] \ n"; echo "$ fields [1] \ n" ;}?>
The above routine will output:
1: PHP Likes dollar signs2: Python Likes whitespace3: Ruby Likes blocks
Like the simple value type generated earlier, key-value pairs generated in an expression context also need to be surrounded by parentheses:
$data = (yield $key => $value);
Generate null value
Yield can be called to generateNULL
Value and an automatic key name.
Generate Example #3NULL
S
The above routine will output:
array(3) { [0]=> NULL [1]=> NULL [2]=> NULL}
Use reference to generate value
The generated function can be generated by reference like a value. This is the same as returning references from functions (a reference is returned from the function): add a reference symbol before the function name.
Example #4 use references to generate values
0) {yield $ value;}/** we can modify the value of $ number in the loop, and the generator uses the reference value to generate, so gen_reference () the internal $ value also changes. */Foreach (gen_reference () as & $ number) {echo (-- $ number). '...';}?>
The above routine will output:
2... 1... 0...
Generator delegation via yield from nation ¶
In PHP 7, generator delegation allows you to yield values from another generator, Traversable object, or array by using the yield from keyword. the outer generator will then yield all values from the inner generator, object, or array until that is no longer valid, after which execution will continue in the outer generator.
If a generator is used with yield from, the yield from expression will also return any value returned by the inner generator.
Example #5 Basic use of yield from
The above routine will output:
1 2 3 4 5 6 7 8 9 10
Example #6 yield from and return values
getReturn();?>
The above routine will output:
1 2 3 4 5 6 7 8 9 10
Comparing generators with Iterator objects
The primary advantage of generators is their simplicity. much less boilerplate code has to be written compared to implementing an Iterator class, and the code is generally much more readable. for example, the following function and class are equivalent:
fileHandle = fopen($fileName, 'r')) { throw new RuntimeException('Couldn\'t open file "' . $fileName . '"'); } } public function rewind() { fseek($this->fileHandle, 0); $this->line = fgets($this->fileHandle); $this->i = 0; } public function valid() { return false !== $this->line; } public function current() { return $this->line; } public function key() { return $this->i; } public function next() { if (false !== $this->line) { $this->line = fgets($this->fileHandle); $this->i++; } } public function __destruct() { fclose($this->fileHandle); }}?>
This flexibility does come at a cost, however: generators are forward-only iterators, and cannot be rewound once iteration has started. this also means that the same generator can't be iterated over multiple times: the generator will need to either be rebuilt by calling the generator function again, or cloned via the clone keyword.
From: http://php.net/manual/zh/language.generators.php
Generator overview (PHP 5 = 5.5.0, PHP 7) generator provides an easier way to implement simple object iteration...