This article introduces the content is about PHP Generator generator Understanding, has a certain reference value, now share to everyone, the need for friends can refer to
Reprinted and collated from: Send where, snow, the Corner, PHP Handbook
Generator (Generator)
Quoted from official website: The generator provides an easier way to implement simple object iterations, comparing the way that the definition class implements the iterator interface, with significantly reduced performance overhead and complexity. The generator allows you to write code in a foreach code block to iterate over a set of data without having to create an array in memory, which will limit your memory to a maximum (focus), or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and the normal function returns only once differently, and the generator can yield multiple times as needed to generate values that need to be iterated.
<?php//Generator Generator implements Iterator { //returns the currently generated value public mixed current (void) //Returns the currently generated key Public mixed key (void) //Generator continues to execute public void next (void) //Reset iterator, which throws an exception if the iteration has already started. Public Void Rewind (void) //pass in a value to the generator, the current yield receives the value, and then proceeds to the next yield public mixed send (mixed $value) / /throws an exception to the generator public void throw (Exception $exception) //Checks if the iterator is closed, returns FALSE if it is turned off, otherwise true public bool Valid (void)// serialization callback public void __wakeup (void) //Return return value of generator function, PHP version 7+ public mixed g Etreturn (void)}?>
The core of the generator function is the yield keyword. Its simplest invocation form looks like a return declaration, except that the normal return returns a value and terminates the execution of the function, and yield returns a value to the code that loops through this generator and simply pauses the execution of the generator function. A generator can not return a value: Doing so produces a compilation error. However, return null is a valid syntax and it will terminate the generator to continue execution.
Yield can be used only in functions, otherwise it will be reported to PHP Fatal error:the "yield" expression can only be used inside a function, any function that uses the yield keyword will return a generat or object. Each time the code executes to the yield statement aborts execution, returns the value of the expression in the yield statement to the generator object, and continues iterating over the generator object, the code after the yield executes until all yield statements have been executed or there is a return statement , this Renturn statement can only return null, that is, return, or it will compile an error.
1 Example One
<?phpfunction Xrang ($start, $end, $step =1) {for ($i = $start; $i <= $end; $i + = $step) {yield $i; The yield keyword defines the break point}}//foreach (Xrang (1, 10000) as $num) {//echo $num. " \ n "; } $rang = Xrang; Var_dump ($rang). Php_eol; Output: Object (Generator) #1 (0) {}var_dump ($rang instanceof Iterator). Php_eol; Output: BOOL (true) $key = $rang->key (); Var_dump ("Key:". $key). Php_eol; Output: String (6) "key:0" $valid = $rang->valid (); Var_dump ("valid:". $valid). Php_eol; Output: String (8) "valid:1" $current = $rang->current (); Var_dump ("Current:". $current). Php_eol; Output: String (Ten) "Current:1" $rang->next (); $key = $rang->key (); Var_dump ("Key:". $key). Php_eol; Output: String (6) "key:1" $valid = $rang->valid (); Var_dump ("valid:". $valid). Php_eol; Output: String (8) "valid:1" $current = $rang->current (); Var_dump ("Current:". $current). Php_eol; Output: String (Ten) "Current:2" $rang->next () $key = $rang->key (); Var_dump ("Key:". $key). Php_eol; Output: String (5) "key:" $valid = $rang->valid (); Var_dump ("valid:". $valid). Php_eol; Output: String (7) "Valid:"//$rang->rewind (); Reset, in all documents currently seen, Rewind () is only implicitly executed the first time the generator is called. The call will throw fatal error after the generator starts iterating.?>
2 Example Two
<?phpfunction Gen () { echo "1111\n"; $ret = (yield ' yield1 '); Var_dump ($ret); echo "2222\n"; $ret = (yield ' yield2 '); Var_dump ($ret); return;} $gen = Gen (); Var_dump ($gen->current ()). php_eol; $a = $gen->send (' Ret1 '); echo "66666\n"; var_dump ($a). Php_eol;echo "77777\n"; Var_dump ($gen->valid ()). php_eol; $b = $gen->send (' Ret2 '); Var_dump ($b). Php_eol;var_dump ($gen->valid ()). Php_eol;//1111//string (6) "Yield1"//string (4) "Ret1"//2222//66666//string (6) "Yield2"//77777//bool (True)//string ( 4) "Ret2"//null//bool (false)?>
2.1 The execution process is:
1. First Call Gen (), enter the function output 1111, execute to the position of the first yield keyword is interrupted (at this time the value of the yield expression is defined "yield1", using current () to get the value of the present expression is a string (6) "Yield1" )
2. Call the Send () method to pass in the value "Ret1" (the value passed into the generator) to the generator. This value will be used as the return value of the yield that the generator is currently in, and the generator starts iterating from the current yield expression, and the program continues to execute
3. Encountered var_dump output the value of the current expression "Ret1", continue to execute output 2222
4. Continue execution, the program comes to the second yield breakpoint, at which point the value of the expression is defined as "yield2" because the call is the Send () method, which returns the value of the current () yield. (See the official documentation for the Send method)
5. $a get the return value of the Send method as "Yield2", continue the output "66666", $a, "77777"
6. Outputs whether the current generator is available
7. Continue execution, pass in the value "Ret2" to the generator, and the generator begins to iterate. At this point the generator is in the second yield expression, which accepts "Ret2" as the return value assigned to the variable $ret, and prints the string (4) "Ret2".
8. After printing,$b = = NULL because it is not thoroughly understood (the question is whether the Send () method returns NULL at this point), guess there are two reasons why:
8.1 Accesses than either may be because there is no break point after the generator, there is no return value (the return value is not allowed, or only return is allowed; return; Used to terminate the execution of the generator), $gen->send () method does not return anything at all, causing $b = = NULL
8.2 The two may be $gen->send (' Ret2 ') after passing in the value, the generator iterates over the yield, implicitly calls next () and current (), and because there is no yield breakpoint under next () causes current () to return null , which causes the Send () return value to be null
8.3 Depending on context, the likelihood of two is greater
2.2 about the Send () method
send () passes in a value to the generator, and as a result of the yield expression, proceeds to the generator. If the generator is not in the yield expression when this method is called, it will run to the first yield expression before passing in the value.