Task #3845

Iterator behaviour - Java vs PHP

Added by Karsten Dambekalns about 12 years ago. Updated almost 11 years ago.

Status:
Resolved
Priority:
Should have
Start date:
2009-07-07
Due date:
% Done:

100%

Estimated time:
Sprint:

Description

From David Buchmann:

iterators are a bit different between java and php. in java, next() advances the iterator one step and then returns the current element. advancing behind the end throws an exception. usually, you do

while(iterator.hasNext()) {
  item = iterator.next();
  ...
}

in php, next() only advances the pointer to the next element, its return type is void. getting the element is done using current(), it is an error to call current() if you are not at an element. to check if next() moved you past the end, there is the valid() method. iterators can be used in the foreach construct, that relies on this semantics. this typically looks like
foreach($iterator as $item) {
  ...
}

PHPCR_IteratorInterface extends the php Iterator. in my opinion, it should follow the php iterator semantics, not the java ones. then it could be used with the foreach construct and it would be less confusing.

we should however define the nextNode() and the other nextXY to follow the java semantics, as they are actually discribed in the jcr
specifications. so they would internally do

nextNode() {
 $this->next();
 return $this->current();
}

and, while criticizing: do we need append method? if the base php iterator does not have it, i do not really see why we should have it here. those iterators are typically used in a search result. we can not add anything to the jcr workspace there, as we do not know where to put it. at least, it should be optional and allowed to throw something like a NotImplemented exception.

#1

Updated by Karsten Dambekalns about 12 years ago

No, from http://php.net/next: "next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one."

The append() is not needed and can probably be dumped. I'll look up why we put that in...

#2

Updated by Karsten Dambekalns about 12 years ago

I mixed up next() (for arrays) and Iterator::next(). The latter is indeed not supposed to return anything. So we should follow suit and make our iterators PHPish in that respect.

As for append() I could not find anything aside from the comment in the sources: append() is something we thought would be nice... So we should drop it again.

#3

Updated by Karsten Dambekalns almost 12 years ago

  • Status changed from Accepted to Resolved
  • % Done changed from 0 to 100

Applied in changeset r3359.

Also available in: Atom PDF