This article introduces to you the content of the PHP dynamic generation of the content of the array (generator) parsing, there is a certain reference value, the need for friends can refer to, I hope to help you.
Defined:
Generator: "An array of dynamically generated content" that is used to produce a value.
Use with: Keyword yield foreach
Process:
The method used as a generator is equivalent to defining an array;
Yield in the generator, each occurrence corresponds to the definition of a value appearing in an array;
foreach iterates through the values of all yield definitions in the generator (a defined value (expression), which is executed only when it is used, and the other expressions between the current yield and the previous yield are not executed);
When foreach ends normally (not break), determine if any other expressions have not been executed after the last yield in the generator, yes, execute;
Unlike threading, which is not parallel execution, it simply switches back and forth between the main business logic and generators, saving only space and time.
Imagine the use scenario:
1, take the big data file, the generator is read by line;
2,???
Example:
gen.php 1 <?php 2 function gen () 3 { 4 echo ' Generator starts execution '. Php_eol; 5 for ($i = 0; $i < 5; $i + +) { 6 echo ' before generating data: '. $i . Php_eol; 7 yield $i; 8 Echo ' After generating the data: '. $i . Php_eol; 9 } echo ' One more data '. Php_eol; yield 5, echo ' Generator execution end '. Php_eol; $gen _func = gen (); Is the echo ' generator starting to execute? ' . Php_eol; ($gen _func as $key = + $val) {+ echo ' before using data '. Php_eol; echo ' Use data: '. $val. Php_eol; echo ' After using data '. Php_eol; //if ($key >= 4) { //break; 23}
Execution Result:
PHP gen.php
Did the generator start executing?
Generator starts execution
Before data is generated: 0
Before using data
Usage data: 0
After using the data
After generating data: 0
Before data is generated: 1
Before using data
Usage data: 1
After using the data
After generating data: 1
Before data is generated: 2
Before using data
Usage data: 2
After using the data
After generating data: 2
Before data is generated: 3
Before using data
Usage data: 3
After using the data
After generating data: 3
Before data is generated: 4
Before using data
Usage data: 4
After using the data
After generating data: 4
One more data.
Before using data
Usage data: 5
After using the data
End of generator execution
Part of the implementation process interpretation:
1, 14 lines do not call the Generator Gen (), just do the definition;
2. After entering the Foreach loop, start calling Gen ();
3, $val need value, remember the current position a, the execution generator;
4, the execution to the yield definition place, takes the value, remembers the current position B, returns the position A;
5, Cycle 3, 42 steps;
6, the end of the cycle, after the completion of the last definition of yield after the remainder of the place;
7, remove 20, 21, 22 lines of comments after the execution, "use data: 4 after using data", there is no additional output.
Others (only PHP7.1.14 versions are verified)
1, $data = (yield $value);//yes $data = yield $value;//no
2, you can use yield $key = = $val;
3. After PHP7, yield from can call generator, array, use return and so on.
(Portal: PHP manual)
Related articles recommended:
New features of the generator in PHP7: Generator delegate (Yield-from) & return value (Return-value)
Simple parsing of PHP generator generators