From the course: Learning the Standard PHP Library

Unlock the full course today

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

Creating a custom filter with CallbackFilterIterator

Creating a custom filter with CallbackFilterIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Creating a custom filter with CallbackFilterIterator

- The CallbackFilterIterator class allows you to create a custom filter using a callback function. It requires a minimum of PHP 5.4. If you're using PHP 5.3, the alternative is to extend the FilterIterator class which is described later in this chapter. I've got open callback_filter.php. It contains the simplexml that was used earlier in this chapter to extract data from courses.xml in the common/data directory. The only difference is that instead of getting the duration, this time we're getting the course level. The first thing we need to do is to convert the simplexml element into a simplexml iterator. We do that by adding a second argument to the simplexml_load_file. So that's on line two and it's a string SimpleXMLIterator. Forgetting to convert simplexml elements into iterators when you want to use them with other iterators is a common problem, so don't forget to do that. So, now we can chain this with the CallbackFilterIterator. I think I'm going to need quite a lot of space so…

Contents