Beware of how works iterator in PHP if you come from Java!
In Java, iterator works like this :
<?phpinterfaceIterator {boolean hasNext();O next();void remove();
}?>But in php, the interface is this (I kept the generics and type because it's easier to understand)
<?phpinterfaceIterator {boolean valid();mixed key();O current();void next();void previous();void rewind();
}?>
1. valid() is more or less the equivalent of hasNext()
2. next() is not the equivalent of java next(). It returns nothing, while Java next() method return the next object, and move to next object in Collections. PHP's next() method will simply move forward.
Here is a sample with an array, first in java, then in php :
<?phpclassArrayIterator implementsIterator {
private finalO[] array;
privateint index=0;
publicArrayIterator(O[] array) {this.array = array;
}
publicboolean hasNext() {
returnindex< array.length;
}
publicO next() {
if ( !hasNext())
throw newNoSuchElementException('at end of array');
return array[index++];
}
publicvoid remove() {
throw newUnsupportedOperationException('remove() not supported in array');
}
}?>
And here is the same in php (using the appropriate function) :
* Since the array is not mutable, it should use an internal
* index over the number of elements for the previous/next
* validation.
*/classArrayIteratorimplementsIterator{
private$array;
public function__construct($array) {
if ( !is_array($array))
throw newIllegalArgumentException('argument 0 is not an array');$this->array= array;$this->rewind();
}
public functionvalid() {
returncurrent($this->array) !==false;// that's the bad method (should use arrays_keys, + index)}
public functionkey() {
returnkey($this->array);
}
public functioncurrent() {
returncurrent($this->array);
}
public functionnext() {
if ($this->valid())
throw newNoSuchElementException('at end of array');next($this->array);
}
public functionprevious() {// fails if current() = first item of arrayprevious($this->array);
}
public functionrewind() {reset($this->array);
}
}?>
The difference is notable : don't expect next() to return something like in Java, instead use current(). This also means that you have to prefetch your collection to set the current() object. For instance, if you try to make a Directory iterator (like the one provided by PECL), rewind should invoke next() to set the first element and so on. (and the constructor should call rewind())
Also, another difference :
<?phpclassArrayIterable implementsIterable {
private finalO[] array;
publicArrayIterable(O[] array) {this.array = array;
}
publicIteratoriterator() {
return newArrayIterator(array);
}
}?>
When using an Iterable, in Java 1.5, you may do such loops :
<?phpfor (String s: newArrayIterable(newString[] {"a","b"})) {
...
}?>Which is the same as :
Iteratorit= newArrayIterable(newString[] {"a","b"});
while (it.hasNext()) {String s=it.next();
...
}?>While in PHP it's not the case :
...
}?>Is the same as :
<?phpfor ($iterator->rewind();$iterator->valid();$iterator->next()) {$current=$iterator->current();
...
}?>
(I think we may also use IteratorAggregate to do it like with Iterable).
Take that in mind if you come from Java.
I hope this explanation is not too long...