5.1. Problem and Solution

For those who have learned PHP and MySQL together hand in hand, the Data Access Object Design Pattern is a new and exciting concept. This Design Pattern aims to solve two specific problems: repetition and data source abstraction.

Programming typically can be a lot of repetition. This was especially true before more popular frameworks started being released. Most PHP programmers can count into the double digits the number of CRUD (create, read, update, delete) applications they've had to make. One of the major portions of repetition in the standard create/update application is the data source manipulation. For the rest of the discussion, I'm going to stop generalizing the data source and refer to it as SQL.

In the application, a SQL statement has to be written to create the entity in the database. Next, an additional SQL statement must be written in order to provide updates to any of the individual features of that entity. The repetition involved in creating these SQL statements is not only boring but also not best practice.

Instead, an object based on the Data Access Object Design Pattern should be created. This Data Access Object (DAO) encapsulates an intelligent way of creating those SQL calls, reducing the complexity and repetition of the entity creation and updating process. It should be written in such a way that the consumers of this object are not aware of the actual table structures or database engine used. Methods that are invoked from this object ...

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.