Why must I rewind IteratorIterator

前端 未结 3 1235
心在旅途
心在旅途 2020-12-21 17:01
$arrayIter = new ArrayIterator( array(1, 2) );
$iterIter = new IteratorIterator($arrayIter);

var_dump($iterIter->valid()); //false
var_dump($arrayIter->valid(         


        
3条回答
  •  猫巷女王i
    2020-12-21 17:27

    While extending the IteratorIterator class to spare implementing the whole iterator interface and/or to create a decorator of an iterator I've been running into this as well.

    That decorator is already the solution to the problem, it only needs to implement the missing functionality to remove the inconsistency. No need for an auto-rewind:

    class IteratorDecorator extends IteratorIterator
    {
        public function valid()
        {
            return $this->getInnerIterator()->valid();
        }
    }
    

    Example: If you have an Iterator object that is valid by default, e.g. ArrayIterator:

    $it = new ArrayIterator(array(1));
    var_dump($it->valid());             # bool(true)
    
    $itit = new IteratorIterator($it);
    var_dump($itit->valid());           # bool(false)
    

    This shows the inconsistency of the IteratorIterator implementation well, the IteratorIterator object does not properly reflect the inner ArrayIterator's state. Using the IteratorDecorator can heal this:

    $decor = new IteratorDecorator($it);
    var_dump($decor->valid());          # bool(true)
    

    And if you have followed up until here, here is another special case you might want to consider: If you don't need to have rewind with the inner iterator, you can just use the NoRewindIterator which returns the validity correct as well:

    $noretit = new NoRewindIterator($it);
    var_dump($noretit->valid());        # bool(true)
    

    Taken Johannes "no auto-rewind" arguments into account, this makes sense, as the NoRewindIterator expects that the iterator should not be rewinded and shows the inner iterator's validity correctly.

    But as the IteratorDecorator shows, I don't do any kind of auto-rewind as well to remove the inconsistency.

提交回复
热议问题