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 ...
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.