php iterators infinite limit glob rewind

PHP Iterators Part 4: SPL Infinite, Limit, Glob and NoRewind Iterators

Continuing our series (PHP Iterators) to demonstrate the SPL PHP Iterators, we will discuss the infinite iterator, the limit iterator, the globe iterator, and the noRewind iterator.



Series Topics:


Infinite Iterator

The infinite iterator is a kinda like an infinite loop that allows to perform operations which requires some recursion or when we need to iterate over an iterator infinitely without stopping.


Note that when using the the infinite iterator it must be a termination condition that terminate the loop otherwise the loop will continue executing infinitely and the browser will crash.

let’s see an example:

In this simple example we created a new instance of the Infinite Iterator passing an array iterator to the constructor then we iterated over it and display each item. After that we added the termination condition like that:

If you remove the termination condition the loop will continue and the browser will crash.



Limit Iterator

The limit iterator is another iterator that always used alongside the infinite iterator and it’s mostly like the limit clause in the mysql database, in other words it allows to iterate over a subset of items in iterator.


The limit iterator constructor takes three parameters:

  • The iterator object
  • the starting offset, if not included it will default to 0.
  • The count of items to limit, if not included it will default to all collection until end.


An example to illustrate the limit iterator:


Also if you remember from above when we said that the infinite iterator needs a termination condition, we can use the limit iterator as the termination condition with the infinite iterator so we can modify the above example like so:


Glob Iterator

The glob iterator as it’s name implies inherits the behavior of the php function glob() to loop over the filesystem files and directories.

As shown in the class structure the glob iterator extends from the File system iterator so each property and method in the File system iterator is also applicable in the glob iterator.


The glob iterator is almost like the glob() function as shown in the below example:

As shown in the example above the GlobIterator constructor takes a pattern to search for files in the same way as glob().


Another example to retrieve all files alongside other  various information, useful if you need to build a file manager in a web UI:

Note that some methods here may throw exception like that:

To solve this exception you need to make sure that the php.ini directive upload_max_filesize and post_max_size has a large value and also post_max_size must be larger than upload_max_filesize.



NoRewind Iterator

In the NoRewind iterator the rewind method never called in the beginning of the iteration or when it’s reversed which means that this iterator never rewound.



To illustrate this behavior consider this example:

If you run the above example you note that the first foreach only executes and output the values but after calling $iterator->rewind() nothing happens and this is because rewind here didn’t reset the loop cursor to be at the beginning.





In this tutorial of this series (PHP Iterators) we covered four more SPL Iterators, The infinite iterator to execute some code infinitely, the limit iterator to work on limited set of collection, the glob iterator to work of filesystem and the noRewind iterator.


Share this: