callback filter iterator caching iterator recursive array iterator

PHP Iterators Part 5: SPL Callback Filter Iterator, Caching, and Recursive Array Iterators

Another common PHP SPL Iterators we will talk about in this part which are callback filter iterator, caching iterator, and recursive array iterator.

 

 

Series Topics:

 

Callback Filter Iterator

The callback filter iterator the same as the filter iterator we talked about previously except that in this time the iterator filter according to a callback function passed as a second argument to the constructor.

As you see that the iterator constructor takes a callback to do the actual job, someone could ask what is the functionality of accept() here, the answer is that accept() just calls the callback passed to the constructor

 

look at the difference between FilterIterator class structure:

The Filter Iterator is declared abstract so to use it you have to extend it while the Callback Filter Iterator doesn’t need to be extend just create an instance and pass the callback and that’s it.

 

The Callback function accept three arguments:

  • the current item
  • the current key
  • the iterator

Let’s see this example:

As shown in the code above we created an instance of the CallbackFilterIterator passing the iterator object (which can any iterator class) in this case ArrayIterator, and the second argument the callback which in this case the function name as string.

 

We can also pass the callback as anonymous function like so:

 

Another example to return the available items:

 

Using the directory iterator along with the callback filter iterator to filter the filesystem:

So as you see the Callback Filter Iterator makes it easy to filter collections without extending the class, just create a new instance and implement the callback function.

 

 

Caching Iterator

The caching iterator offers a mechanism to support a cached iteration over another iterator. After that using a special method getCache() we can retrieve the contents of the cache.

 

Caching Iterator Constants:

  • CALL_TOSTRING: Convert every element to string
  • CATCH_GET_CHILD: When accessing a children don’t throw exception
  • TOSTRING_USE_KEY: Use CachingIterator::key() for conversion to string.
  • TOSTRING_USE_CURRENT: use CachingIterator::current() for conversion to string.
  • TOSTRING_USE_INNER: use CachingIterator::getInnerIterator() for conversion to string.
  • FULL_CACHE: Cache all readable data.

 

One important note when using the Caching Iterator is that the caching done during iteration on the data which means if you create the caching iterator instance and then call getCache() directly it will be empty as shown in this example:

So to retrieve the cached data correctly you have to loop over the data first in order for the iteration to be saved in cache

 

When adding items into the Cached Iterator it’s important to use the flagCachingIterator::FULL_CACHEotherwise you encounter this exception:

 

Fatal error: Uncaught BadMethodCallException: CachingIterator does not use a full cache (see CachingIterator::__construct)

 

Recursive Array Iterator

This iterator is the same as array iterator as it allows to unset and modify values and keys while iterating over Arrays and Objects, in addition to that it can iterate over the current iterator entry.

 

The recursive array iterator is most powerful when dealing with multidimensional arrays or arrays which has inconsistent structure like arrays that contact other singular and child arrays.

 

A real world example to demonstrate this iterator, consider we have a navigation menu that represent website navigation like this:

Now let’s pass this array to the Recursive array iterator like so:

 

If you run the above code you will see that the iterator displays the items in hierarchy like this:

 

Now to traverse all the items we can create a function that will be called recursively passing the iterator object like this one:

In the above function all we iterated over the iterator using a while loop and then checked if the iterator has children using RecursiveArrayIterator::hasChildren(), if it has we called the function again passing the child iterator (RecursiveArrayIterator::getChildren()), otherwise we displayed the items (key and value).

 

Conclusion

We talked in this tutorial of our series (PHP Iterators) about three more SPL Iterators which are CallbeckFilterIterator for filter arrays using a callback, CachingIterator for caching iterations, and RecursiveArrayIterator for manipulating collections in a recursive manner.

 

Share this: