A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearWhich implementation of Iterator should I use in PHP, and why? - page 1
User InfoPosts
Which implementation of Iterator should I use in PHP, and why?#1
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?

posted date: 2008-12-11 03:55:00


Re: Which implementation of Iterator should I use in PHP, and why?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 03:55:01


Re: Which implementation of Iterator should I use in PHP, and why?#3
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.

posted date: 2008-12-11 04:01:00


Re: Which implementation of Iterator should I use in PHP, and why?#4
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.

posted date: 2008-12-11 04:04:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.