views:

84

answers:

2

If I want to implement seek() to complete the SeekableIterator interface, should I internally revert to the old position if the seeked position is invalid? Like this:

public function seek( $position )
{
    $oldPosition = $this->_position;
    $this->_position = $position;
    if( !$this->valid() )
    {
        $this->_position = $oldPosition;
        throw new OutOfBoundsException( 'Invalid seek position (' . $position . ')' );
    }
}
+2  A: 

If you use the php.net interface reference's example implementation as a guideline, then no, you should not "revert" to the original position, even if the supplied target position isn't valid.

Amber
A: 

According to the sample code from the SPL documentation, you should still change the position.

carl
I see we had the same idea. ;)
Amber
Clearly great minds think alike.
carl