2

I have a class and I want to be able to iterate over a certain array member. I did a quick search and found IteratorAggregate:

class foo implements IteratorAggregate
{
    protected $_array = array('foo'=>'bar', 'baz'=>'quux');

    public function getIterator()
    {
        return new ArrayIterator($this->_array);
    }
}

which works great, but doesn't that create a new ArrayIterator instance every time foreach is used on it?

So I thought I should store the iterator instance in a member:

    protected $_iterator;

    public function getIterator()
    {
        if (!$this->_iterator instanceof ArrayIterator) {
            $this->_iterator = new ArrayIterator($this->_array);
        }
        return $this->_iterator;
    }

The problem is that the iterator uses a copy of $this->_array during the first call of getIterator(), so changes to the member aren't reflected on subsequent foreach constructs.

I was thinking I should subclass ArrayIterator, add a setArray($array) method and call it before returning it in getIterator(), but I don't know the member name of the array it uses internally and whether or not it's overwriteable by a subclass.

The question is: is this a premature and/or unnecessary optimization? If no, what's the best way to achieve this?

2 Answers 2

4

You second code will fail on multiple loops of the object where you start a new loop before another is finished.

Didnt really look like an optimization worth spending time on either.

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

1 Comment

I see, I haven't considered that possibility.
4

but doesn't that create a new ArrayIterator instance every time foreach is used on it?

yes, and?

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.