The existing ArrayIterator has a large API surface. In most cases it
will duplicate the input array either out of ease of implementation or
as consequence of the API. Given that a significant use case for
ArrayIterator is to write generic code for Iterators that also work on
arrays, it is a pretty big drawback to always duplicate the array.
Theoretically, I'd like to deprecate basically everything on
ArrayIterator except the Countable, Iterator interfaces. This is based
on the observation that ArrayObject will do basically all of the same
stuff in a more obvious way, so we don't need it for ArrayIterator.
Internally, they are basically the same class except for the
differences in Iterator and IteratorAggregate APIs. Then, in PHP 9.0
we could make this more efficient.
However, I don't want to wait that long, and I suspect it would have
too much code churn to remove nicely. The alternative is to make a new
array iterator class that has a simple API that avoids duplication of
the array. I'm not sure what to name it:
- Spl\ArrayIterator - It's a bit confusing, especially because
technically \ArrayIterator is in the SPL. - ArrayValueIterator - Its name suggests it is only the array's
values that are iterated over, and would skip keys. This was suggested
simultaneously with ArrayReferenceIterator, which would hold the inner
array by reference. In that context the name makes a bit more sense,
but I don't know what use cases there are that wouldn't be served by
existing ArrayObject/ArrayIterator. - SimpleArrayIterator?
- ForwardArrayIterator? This would go along nicely with a
ReverseArrayIterator, I guess.
Anyone other ideas?