Chapter 11. Iterator Pattern

One of the most valuable things a computer can do is execute a repetitive task. Always doing it the same way without becoming "bored" or getting tired, a computer can chug along doing the same thing over and over. This is one of the many reasons why computers became mainstream, affordable, and a staple in the household. Their ability to do simple things repetitively is amazing: anything from simple math to playing music on repeat or helping to correct a misspelled word in this book over and over. Computer programming languages manage repetition through a construction called a loop. Looping is used in almost every program and action now, without our even realizing it. From a programming point of view, however, not all objects are the same when it comes to looping. Some require complicated hash table access. Others can be handled like an array. Because of this complexity in object interface, a common method of looping through items had to be established. This is where the Iterator Design Pattern stands alone.

Name: Iterator

The Iterator Design Pattern helps construct objects that can provide a single standard interface to loop or iterate through any type of countable data.

Get Professional PHP Design Patterns 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.