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.

Creating a custom filter with RecursiveFilterIterator

Creating a custom filter with RecursiveFilterIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Creating a custom filter with RecursiveFilterIterator

- To filter a recursive iterator with a custom class, you need to extend RecursiveFilterIterator and define it's accept method. This needs to return true not only when the filter condition is met but also when the current element has children. This is recursive_filter01.php, which you can find in the chapter three folder of the exercise files. It creates an instance of RecursiveDirectoryIterator to traverse the file structure from the current directory and sets the option to use UNIX_PATHS. To keep things simple, I'm going to create the custom class in the same file as this, so I'm going to add a few lines at the top and we'll create a class that filters images by checking their file name extension, so class and we'll call it FilterImages and it extends RecursiveFilterIterator. Unless you want to override the constructor method, you only need to define the accept method and overriding the constructor method of a recursive iterator is slightly complicated so I'm going to deal with that…

Contents