From the course: Learning the Standard PHP Library

Unlock the full course today

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

Extending FilterIterator to create a reusable filter

Extending FilterIterator to create a reusable filter - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Extending FilterIterator to create a reusable filter

- Filter Iterator is different from most of the SPL Iterators in that you can't create an instance of Filter Iterator itself. It's an abstract class. So, you have to create your own class and extant the Filter Iterator class. To show you how to use it, I'm going to be using the same basic simple XML to extract data from courses.XML that I've used in other videos in this chapter. You can find a copy of this file filter.php in the Chapter 3 folder of the exercise files. Normally, it's considered best practice to create class definitions in a separate file, but for the purposes for this demonstration, I'm going to create it at the top of this page here. So, I'm going to add in a few extra lines. I'm going to create a class that will filter the XML by author. So, class. We'll call it Author Filter, and it extends Filter Iterator. We'll begin by creating a protective property to store the authors name. Normally, the only method you need to define in the classes extend Filter Iterator is a…

Contents