Iterating over a large amount of data using a loop structure (such as foreach) will require a lot of memory and considerable processing time. Use Generators to iterate over a set of data without this overhead. Generator functions are like ordinary functions. However, instead of a return statement in the function, the generator is executed repeatedly using the yield keyword to provide the values to be iterated over.
The yield keyword is the core of the generator mechanism. Although its usage looks similar to return, it does not stop function execution. It provides the next value of the iteration and pauses the execution of the function.
Generator implements Iterator { /* Methods */ public current ( void ) : mixed public getReturn ( void ) : mixed public key ( void ) : mixed public next ( void ) : void public rewind ( void ) : void public send ( mixed $value ) : mixed public throw ( Throwable $exception ) : mixed public valid ( void ) : bool public __wakeup ( void ) : void }
public Generator::current (void) − mix — Get the generated value
public Generator::getReturn ( void ) : mix — Get the return value of the generator
public Generator::key ( void ) − mix — Get the key of the generated value.
p>public Generator::next ( void ) − void — Resumes execution of the generator. The effect is the same as calling Generator::send() with NULL as argument.
public Generator::rewind ( void ) − void — Rewind the iterator. This will throw an exception if the iteration has already started.
public Generator::send (mixed $value) : mix - Sends the given value to the generator as the result of the current yield expression and restores the generator.
public Generator::throw ( Throwable $exception ) − mix — Throws an exception into the generator and resumes execution of the generator.
public Generator::valid ( void ) − bool — Check if the iterator has been closed
public Generator::__wakeup ( void ) − void — Exception thrown because the generator cannot be serialized.
The Generator class implements the Iterator interface. Generator objects cannot be instantiated via new. Any user-defined function with the yield keyword creates an object of the generator class.
Since the generator implements the Iterator interface, each loop can be used to iterate over the generated values.
Live demonstration
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); foreach ($gen as $val){ echo $val . " "; } ?>
The above program displays the following output
1 4 9 16 25
The following example uses the current() and next() methods of the generator class to Traverse the generated values. Use the valid() method to check loop conditions.
Real-time demonstration
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); while ( $gen->valid() ){ echo "key: " . $gen->key(). " value: ". $gen->current() . ""; $gen->next(); } ?>
The above program displays the following output
key: 0 value: 1 key: 1 value: 4 key: 2 value: 9 key: 3 value: 16 key: 4 value: 25
The above is the detailed content of PHP generator class. For more information, please follow other related articles on the PHP Chinese website!