Today we will learn a new topic of generators in PHP in this PHP Generators tutorial.
- The PHP 5.5 has a good support for generators.
- It is not a new concept; rather it is a very powerful feature in languages like ruby, C# and python.
- So it is also used in PHP to provide an easy way to implement iterators.
- Generators are used to provide an easy way to implement iterators without the overhead or complexity of implementing a class that implements an iterator.
- Along with this complex task it also allows us to save a lot of memory.
- If we implement a simple function to return a large number of values, it is defined in such a way that it generates all the values and then returns them at once in an array.
- But using generators we can return each value at the same time it is generated.
- For this a generator function and a yield keyword plays a very important role.
- So let us see what this generator is and how does it work?
- Working of a generator:
- To use a generator we define a generator function and return the value from it using a yield keyword.
- What is this generator function? A generator function is just a simple normal function; but instead of returning a value using normal return statement, it yields (returns) a value using yield statement. It looks like a function but acts like an iterator.
- What is the task of yield keyword? The yield keyword in a generator function returns the value to the caller of the function. But instead of terminating the function or removing it from stack, it saves its state on the stack and whenever the function is called again it continues from where it was according to the saved state.
- When actually the generator function is called it returns an object of Generator class which implements an Iterator interface. This object is iterated over again and again till the generator function is repeatedly called.
- The normal return statement cannot return any value from a generator function, but it can be used without any value to terminate its execution.
- Let us understand it with an example.
- We are going to write a generator function to return numbers upto the given range and display them.
- To demonstrate it, let us create a new folder named generators in the htdocs folder in xampp folder. Then open a new notepad++ document and save it as index.php in this newly created generators folder.
- Write the following code in index.php file:
<?php function nos($l) { for($i=1;$i<=$l;$i++) { yield $i; } return; } echo "<h1>Numbers upto the given range are:</h1>"; foreach(nos(10) as $n) { echo $n."<br>"; } ?>
foreach(nos(10) as $n)
- Generators will be useful in situations where you want a very large number of values to be handled without any complex task of handling iterators.
- More importantly when you want to display the values and there is no compulsion of storing the values in memory. This saves a lot of memory. For example, reading a file.
- Generators not just return values, they can even accept values with the yield keyword.
- The method send() is used to pass value to the yield. The send() method of the generator object is called with the desired parameter.
- Let us see an example: If we want our above program with fig 1 to display only first 5 nos. instead of 10, write the following code in index.php file:
<?php function nos($l) { for($i=1;$i<=$l;$i++) { $e=yield $i; if($e=='end') return; } } echo "<h1>Numbers upto the given range are:</h1>"; $no=nos(10); foreach($no as $n) { if($n==5) $no->send('end'); echo $n."<br>"; } ?>
Thus we studied generators in a simple way in this PHP Generators tutorial.