1

I am trying to understand the behaviour of FilterIterator at This Code, I was trying to understand the action sequences, I didnt understand why if you try to print the current() value it wont work unless you will use next() or rewind() before for example:

// Please take a look at the link before
echo $cull->current(); // wont work
$cull->next(); or $cull->rewind(); then echo $cull->current(); // work

Now i dont know what i have to "Refresh" the "Pointer" to be able to print elements, if can some one explain to me please the action sequences mabye it will become clearer, Thank you all and have a nice day.

1
  • Why did you link to an external website for your code? Posting code here is highly encouraged. Commented Oct 14, 2012 at 21:47

2 Answers 2

1

imho if you don't call next() or rewind before accessing current() at first time, the internal iterator-pointer is not set to the first element...

common scenario is while($it->next()) AFAIK!

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain to me the action sequences from my example at the link.
1

This is the same question I asked here, even though it sounds different: Why must I rewind IteratorIterator (Your CullingIterator is a FilterIterator which is an IteratorIterator).

Read the accepted answer and the comments, but the summary is that the way IteratorIterator is written in the php source code, functionally models something like this:

class IteratorIterator {
    private $cachedCurrentValue;
    private $innerIterator;
    ...
    public function current() { return $this->cachedCurrentValue; }
    public function next() {
        $this->innerIterator->next();
        $this->cachedCurrentValue = $this->innerIterator->current();
    }
    public function rewind() {
        $this->innerIterator->rewind();
        $this->cachedCurrentValue = $this->innerIterator->current();
    }
}

The important part being that the value is NOT retrieved from the inner iterator when you call current(), but instead, it's retrieved at other times(and the constructor isnt one of them).

Personally, I think this a borderline a bug, because it's unexpected and solvable without introducing unwanted behavior or performance issues, but oh well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.