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 - PHP Tutorial
From the course: Learning the Standard PHP Library
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Filtering a directory with GlobIterator5m 14s
-
(Locked)
Using regular expressions to filter directories with RegexIterator3m 49s
-
(Locked)
Extracting data from XML with RegexIterator and SimpleXML6m 25s
-
(Locked)
Specifying a range of items with LimitIterator4m 21s
-
(Locked)
Creating a custom filter with CallbackFilterIterator5m 6s
-
(Locked)
Creating a recursive callback filter5m 42s
-
(Locked)
Extending FilterIterator to create a reusable filter5m 10s
-
(Locked)
Creating a custom filter with RecursiveFilterIterator4m 48s
-
(Locked)
Overriding the RecursiveFilterIterator constructor5m 16s
-
(Locked)
Using ParentIterator to find elements that have children3m 59s
-
(Locked)
-
-
-
-
-