
Building RESTful Web Services with PHP 7
By :

Although came in PHP5.5, most PHP developers don't use them and most probably do not know about generators. So, let's first discuss generators.
As stated in the PHP manual:
Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the iterator interface.
OK, here is a more detailed and easy-to-understand definition from the same source, php.net:
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate. Instead, you can write a generator function, which is the same as a normal function, except that instead of returning once, a generator can yield as many times as it needs to in order to provide the values to be iterated over.
For example, you can simply write a function returning a of different...