Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While extending the <code>IteratorIterator</code> 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.</p> <p>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:</p> <pre><code>class IteratorDecorator extends IteratorIterator { public function valid() { return $this-&gt;getInnerIterator()-&gt;valid(); } } </code></pre> <p>Example: If you have an <code>Iterator</code> object that is valid by default, e.g. <code>ArrayIterator</code>:</p> <pre><code>$it = new ArrayIterator(array(1)); var_dump($it-&gt;valid()); # bool(true) $itit = new IteratorIterator($it); var_dump($itit-&gt;valid()); # bool(false) </code></pre> <p>This shows the inconsistency of the <code>IteratorIterator</code> implementation well, the <code>IteratorIterator</code> object does not properly reflect the inner <code>ArrayIterator</code>'s state. Using the <code>IteratorDecorator</code> can heal this:</p> <pre><code>$decor = new IteratorDecorator($it); var_dump($decor-&gt;valid()); # bool(true) </code></pre> <p>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 <code>rewind</code> with the inner iterator, you can just use the <code>NoRewindIterator</code> which returns the validity correct as well:</p> <pre><code>$noretit = new NoRewindIterator($it); var_dump($noretit-&gt;valid()); # bool(true) </code></pre> <p>Taken Johannes "no auto-rewind" arguments into account, this makes sense, as the <code>NoRewindIterator</code> expects that the iterator should not be rewinded and shows the inner iterator's validity correctly.</p> <p>But as the <code>IteratorDecorator</code> shows, I don't do any kind of auto-rewind as well to remove the inconsistency.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload