Nested menu with PHP’s RecursiveIteratorIterator

The Standard PHP Library (SPL) has been around for years now, yet we don’t see them being used in our daily codes. Frameworks like Symfony2, Laravel use them internally. Today I am writing about one of the key members of the SPL, the RecursiveIteratorIterator, and implement something web developers always have to deal with, nested menus.

As the name suggests, it is used to iterate through a recursive iterator. There are many recursive iterators provided in SPL like RecursiveArrayIterator and RecursiveDirectoryIterator. A recursive iterator should implement the RecursiveIterator interface. There is not much documentation about these in the PHP website, but there are examples you can learn from.

If you have never used any of the Iterators, you can think of them as ‘loopables’.  The `RecursiveIterator` interface extends `Iterator` which extends `Traversable`, which is described as “Interface to detect if a class is traversable using foreach“, meaning iterators can be directly passed to foreach and looped. Continue reading Nested menu with PHP’s RecursiveIteratorIterator