Extending an Iterator to use an Iterator to make your code a little cleaner

Monday March 11th, 2013


One of the nice things about iterators is the ability to shove them into other iterators, allowing you to wrap their functionality in other functionality to return a more precise result set. Take for example the idea that we want to read a directory to list only the images inside of it. There are two main ways to do this, via opendir / readdir functions and via FilesystemIterator objects.

Going the FilesystemIterator route, one common way seems to be extend another class called FilterIterator which allows you to customize a filter based on you overwriting a method called accept().

Armed with that class, we could have a bit of code like this…

Which is fine, but if I need to use this ImageFilterIterator multiple times in a code base, not only does that look clumsy but it is super annoying to type. We can clean that up a bit if we extend FilterIterator a little more. Instead of just overriding the accept method, we will also override the __construct.

Now the code that needs this functionality can be reduced down to…

This I find much more tolerable – we have the full power of the FilterIterator running a FilesystemIterator without having to specify that every time we want to read the image directory.

Comment by hakre on Tuesday March 12th at 11:08am
There is also the good old RegexIterator wich can filter based on a regex against current() or key() - http://www.php.net/class.regexiterator - don't miss it ;)
    Comment by bob on Tuesday March 12th at 11:28am

    yep, it's nice. i elected to go with straight filter in this example since regex on the filename isn't a solid test of if the files are images. the accept methods here should be made more in depth for serious use :D
Comment by ashalaenko on Tuesday March 26th at 11:20am
I think in the last example we lost flexibility. What happend when we will get an array of images? Should we recode iterator or create new iterato for this case? In first example we can specify iterator manually and this much better as I think. Sorry for English
    Comment by bob on Tuesday March 26th at 11:32am
    @ashalaenko: then do what you need to do! :)

    the application that i was working on that inspired this will always be reading them from the file system, because that is its one job. this is also only a small example of things you can do, there are lots of iterator classes that can be combined to become useful utilities.
Comment by louis vuitton 楽天 on Wednesday August 14th at 8:49pm
楽天 louis vuitton
Comment by homepage on Monday August 25th at 8:51am
Thank you a bunch for sharing this with all folks you really realize what you are talking approximately!

Bookmarked. Kindly also discuss with my web site =). We will have a link trade agreement among us
Comment by chaturbate token on Thursday October 2nd at 4:54am
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You definitely know what youre talking about, why waste your
intelligence on just posting videos to your blog when you could
be giving us something enlightening to read?
Comment by liber.hse.perm.ru on Monday October 13th at 11:22am
This is my first time pay a quick visit at here and i am genuinely impressed to read everthing at one place.
Comment by gingerpills on Friday January 30th at 7:04pm
We stumbled over here by a different web page and thought I might check things out.
I like what I see soo now i am following you. Look forward to checking out your web page for a second time.
9 Responses to “Extending an Iterator to use an Iterator to make your code a little cleaner”
Write a Comment