© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_19

19. Iterator

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout iterator

Intent

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.1

Applications

What is an aggregate object? The word aggregate means a whole formed by combining several elements. In programming terms, it’s a collection, array, list, etc. Why use an aggregate object, though? You can already aggregate a bunch of numbers, strings, or objects all sequentially using arrays. Why do you need the iterator pattern? Is this a useless pattern? The answer is no.

There are some benefits you gain from using an iterator object. ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.