views:

243

answers:

2

I'm trying to refactor a large, old project and one thing I've noticed is a range of different Iterator implementations:

while($iterator->moveNext()) {
    $item = $iterator->current();
    // do something with $item;
}   

for($iterator = getIterator(), $iterator->HasNext()) {
    $item = $iterator->Next();
    // do something with $item
}   

while($item = $iterator->fetch()) {
    // do something with item
}

or even the StandardPHPLibrary (SPL) iterator which allows

foreach($iterator as $item) {
    // do something with $item
}

Having so many different Iterators (with different methods for looping over collections) seems like a strong code smell, and I'm inclined to refactor everything to SPL. Is there a compelling advantage to any of these implementations of Iterator, or is it purely a matter of personal taste?

+8  A: 

The SPL version is definitely the way to go. Not only is it the easiest to read, but it's a part of PHP now, so will be familiar to many more people.

There's nothing "wrong" with the others, but as you stated, having all these different versions in one project isn't helping anyone.

Greg
+1  A: 

Imo, simply utilising one or more of the SPL libraries as an interface tends to be less ugly in use at the front end. However, the backing behind the implementation can get a bit ugly.

For instance, I wrote an iterator that efficiently iterated a database result set, so that results that were never requested were never fetched from the request pointer, and if items were prematurely fetched ( IE: $obj[ 5 ] ) , it would seek all the required results into an internal buffer.

Worked wonderfully, you just pray that code that makes the magic behind the scenes never fails because it confuses people when they see you use something that looks like an array, and it does "magic" which can fail :)

Magic got people burnt at the stake. So use it carefully and wisely, possibly make it obvious how it works.

My personal preference is for the

for( $object as $i => $v )

notation for it is generally more consistent and predictable.

for( $dbresult->iterator() as $i => $v ){ 

}

style notation is functionally identical, but at least you have less guesswork how it works on the surface.

Kent Fredric