News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Bob Majdak:
Extending an Iterator to use an Iterator to make your code a little cleaner
March 12, 2013 @ 09:25:04

In this new post to his site Bob Majdak talks about extending iterators to help make it easier to customize it for your needs.

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 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().

He shows not only overriding the "accept" method, but also the constructor to make using this new iterator a much simpler (and cleaner) call. You can find out more about the FilesystemIterator (and others) over in the Iterators section of the PHP manual.

0 comments voice your opinion now!
extend iterator clean code accept constructor filesystemiterator


blog comments powered by Disqus

Similar Posts

Timothy Boronczyk's Blog: Spaghetti Code Considered Harmful

Zend Developer Zone: Extending Zend Server GUI – Data Cache Information

Alvaro Videla's Blog: Reply to "Scala is Easier than PHP"

Procurios Blog: Syntactic Sugar for MySQLi Results using SPL Iterators

Jani Hartikainen: How to make your code self-documenting?


Community Events

Don't see your event here?
Let us know!


language release conference php7 laravel framework podcast example unittest opinion laravel5 extension interview introduction voicesoftheelephpant community library api series version

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework