What are PHP iterators and the Iterator/IteratorAggregate interfaces used for?

Last updated 15 June 2026

  • Iterators let an object define how foreach traverses it, hiding internal storage behind an array-like interface.

  • Iterator requires five methods (current(), key(), next(), rewind(), valid()) for full control over lazy or custom traversal.

  • IteratorAggregate needs only getIterator(), returning an existing iterator such as ArrayIterator or a generator.

  • https://www.php.net/manual/en/class.iteratoraggregate.php
  • https://stackoverflow.com/questions/4626732/merge-array-items-into-string
All PHP interview questions