O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.1. Problem and Solution

One of the most convincing proofs that these Design Patterns make sense is their appearance in the continued refactoring of any of the older code a programmer creates. I fondly remember creating my first data-driven site. It used flat files to handle the data and was pretty clunky and slow. When I learned MySQL, I migrated all of the data into a few database tables. I still had to write multiple queries to return my data. When I progressed to an object-oriented refactor, I started creating data objects from those MySQL queries. I still created each individual object by hand and then accessed them later. Finally, the last step I remember was creating an object that created objects for a page. It had a method to get the next object from the list. Little did I know it — I was on the edge of actually implementing a Design Pattern: the Iterator Design Pattern.

The Iterator Design Pattern helps fashion objects to handle these collections of data or other objects. When creating a class based on the Iterator Design Pattern, a set of interfaces is created in order to provide a unified approach to managing these collections. For example, some objects dictate the mere creation of themselves as evidence that they are complete and available to the collection. Other objects may require additional building before they are available to be processed as part of the collection. The class based on the Iterator Design Pattern will provide those unified public methods to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required