From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Extending RecursiveIteratorIterator to build nested lists

Extending RecursiveIteratorIterator to build nested lists - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Extending RecursiveIteratorIterator to build nested lists

- A really cool feature of RecursiveIteratorIterator is that you can override methods that are called automatically, not only at the beginning and the end of a loop, but also at the beginning and end of looping over child elements. So let's extend RecursiveIteratorIterator to convert this multidimensional associative array into a series of nested unordered lists. This is recursive_extend.php which you can find in the chapter four folder of the exercise files. Although its normally considered best practice to define classes in a separate file, I'm going to do it in the current file for the purposes of this exercise. So let's just add in a couple of extra lines at the top, and we're going to be creating an unordered list so we'll call the class UnorderedListBuilder, and it extends RecursiveIteratorIterator. We're going to override four public methods, beginIteration, endIteration, beginChildren, and endChildren. As the name suggests, these methods are called the beginning and end of the…

Contents