Iterator design pattern in PHP

Iterator design pattern is a software design pattern which provides access to the elements of an aggregate object sequentially without exposing its underlying representation. It makes elements appear as a collection of objects.

  • Promote to "full object status" the traversal of a collection.
  • Polymorphic traversal

PHP implementations

  • PHP offers Iterator interface out of the box. Its SPL library has also a wide variety of useful iterators. After understanding the basics of the iterator pattern using these instead of reinventing the wheel is more convenient.

See also



Found a typo? Something wrong with this content?

Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all contributors.