
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Generator Class
Introduction
Traversing a big collection of data using looping construct such as foreach would require large memory and considerable processing time. With generators it is possible to iterate over a set of data without these overheads. A generator function is similar to a normal function. However, instead of return statement in a function, generator uses yield keyword to be executed repeatedly so that it provides values to be iterated.
The yield keyword is the heart of generator mechanism. Even though its use appears to be similar to return, it doesn't stop execution of function. It provides next value for iteration and pauses execution of function.
Syntax
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 }
Methods
public Generator::current ( void ) − mixed — Get the yielded value
public Generator::getReturn ( void ) : mixed — Get the return value of a generator
public Generator::key ( void ) − mixed — Gets the key of the yielded value.
public Generator::next ( void ) − void — Resume execution of the generator. same effect as calling Generator::send() with NULL as argument.
public Generator::rewind ( void ) − void — Rewind the iterator. If iteration has already begun, this will throw an exception.
public Generator::send ( mixed $value ) : mixed — Sends given value to generator as result of current yield expression and resumes generator.
public Generator::throw ( Throwable $exception ) − mixed — 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 — Throws an exception as generators can't be serialized.
Generator class implements Iterator interface. Generator objects cannot be instantiated via new. Any user defined function having yield keyword creates object of generator class.
Generator Example
Since generator implements Iterator interface, for each loop can be used to traverse yielded values.
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); foreach ($gen as $val){ echo $val . " "; } ?>
Output
Above program shows following output
1 4 9 16 25
following example uses current() and next() methods of generator class to traverse yielded values. Loop condision is checked with valid() method.
Example
<?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(); } ?>
Output
Above program shows following output
key: 0 value: 1 key: 1 value: 4 key: 2 value: 9 key: 3 value: 16 key: 4 value: 25