Generators overview

(PHP 5 >= 5.5.0, PHP 7)

Generators provide an easy way to implement simple
iterators without the
overhead or complexity of implementing a class that implements the
Iterator interface.

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.

A simple example of this is to reimplement the range()
function as a generator. The standard range() function
has to generate an array with every value in it and return it, which can
result in large arrays: for example, calling
range(0, 1000000) will result in well over 100 MB of
memory being used.

As an alternative, we can implement an xrange()
generator, which will only ever need enough memory to create an
Iterator object and track the current state of the
generator internally, which turns out to be less than 1 kilobyte.

When a generator function is called for the first time, an object of the
internal Generator class is returned. This object
implements the Iterator interface in much the same
way as a forward-only iterator object would, and provides methods that can
be called to manipulate the state of the generator, including sending
values to and returning values from it.

Bear in mind that execution of a generator function is postponed until iteration over its result (the Generator object) begins. This might confuse one if the result of a generator is assigned to a variable instead of immediate iteration.