PHP

PHP like a Boss: Understanding Generators


Yeah, maybe it was the generator cover image but I am glad you are here :smile: Now what are Generators and how do they work in PHP?

Generators are special routines that can be used to control the iteration behaviour of a loop. Generators are used in the implementation of fibonacci.

Visit here to have the overview of these implementation.

Generators are similar to Iterators. For many PHP Programmers, Iterators have been about the only choice until the release of PHP 5. I am guessing that this is the reason why we do not have as many write ups in generators as we have with Iterators. While generators are unpopular in the PHP language, it’s not a new concept. They already exist in languages such as C#, Python, Javascript and Ruby (enumerables).

A brief overview of Iterators: They help in looping/traversing through an array. The keyword “foreach” is mostly used when iterating.

However, Generators provide an easy way to implement simple Iterators. Much less boilerplate code is written and the code is generally more readable compared to the Iterators.

In this article, I am going to share with you a very simple implementation of generators. If you are ready, I am ready, then let’s dive in:

Simple Example

Firstly, the generators are declared just like functions. An iterator is then used with the ‘yield’ keyword instead of ‘return’. The generators are objects except that they cannot be instantiated with the ‘new’ keyword. Then followed by another foreach loop which echoes the square of the array as seen in the output.

Printing Keys and Values

We can easily define a generator function which returns a key and a value.

Generators also allow us to print out the keys and the values of an array. In the above example, we yielded the key and the value like this: “yield $key => [$val]”. Outside the function, while calling the generator object, we passed in the array. The for loop then iterates over the object and prints out the value as seen above.

Generator function by reference

We can define a generator function that the values yielded are returned by reference

Generator  returns Expression

PHP 7 allows us to include a return statement within a generator function in order for the final expression to be returned. The final expression can be fetched by calling the getReturn() function on the generator object. The possibility to return a value from a generator allows us to get the return of a calculation that is executed inside the generator function.

Let’s dive in:

Generator  delegation

PHP 7 also allows us to yield an expression inside the generator function. From the example below, the iteration occurs within the generator function and the result of the iteration got yielded inside the generator.

In PHP 7, generator allows us to yield values from an array using the keyword ‘yield from’ as seen in the above example. The generator object will yield all the values inside the array. As seen in the above example, the generator yielded all the values in the two arrays declared inside the function.

Conclusion

For every Iteration/looping you need to do, consider making use of the powerful tool called generators. With the introduction of generators, we can write a more readable code that iterates and simultaneously saves us memory. The elePHPant giant has blessed us with a powerful weapon, we should take huge advantage of it.

Please if you have any questions or observations, let me know in the comments section.

Ademola Raimi

About Ademola Raimi

A Technopreneur, Software Developer at @andela. Passionate about solving problems. Love to watch Scifi movies.