Code 10x More Efficiently Using Data Access Objects: Part 1 (Page 1 of 6 )
Everyone knows that today an effective website cannot do without dynamic content. Usually this content comes from some kind of database. PHP provides great built-in functions that allow the developer or programmer to access all of the most popular (and many of the not so popular) databases. There is not much work involved in connecting to a database, sending a SQL query over, receiving some kind of data back and then iterating over the returned records and … doing something interesting with it. Sounds pretty straight forward, doesn't it? I guess it does until you need to do the same thing over and over on just about every page, sometimes more than once on a single page, and it becomes even more interesting if you need to access different databases within one script.
One thing that signifies every good programmer is the desire to make coding life easier for him or herself. The first step is to create a reusable chunk of code that can be copied and pasted over and over, with changing parts of it as needed (a technique known as "code reusability). The second step would be creating a function that encapsulates some of this functionality, but ultimately is still not flexible enough to provide the desired reusability.
There must be a better way. And there is! Why not use the Object Oriented Programming capabilities of PHP and encapsulate the functionality that every developer uses about 95% of the time? Let's use a Data Access Object (DAO) for our communication with a database. (For experienced programmers: Don't worry, I am not talking about database abstraction such as PEAR::DB, so read on…)