2. Motoma
This article is cross posted from my personal blog. You can find the original article, in all its
splendor, at http://motomastyle.com/creating-a-my...-layer-in-php/.
Introduction:
The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us
to ignore the intricacies of MySQL and focus our attention on our Application Layer and
Business Logic. Hopefully, by the end of this guide, you will have a working DAL and learn a
little about PHP, MySQL, and Object-Oriented design in the process.
Assumptions:
You know PHP and have is set up.
You know MySQL and have it set up.
You have a cursory knowledge of object-oriented methodologies.
You are not a smacktard.
Defining the Project:
The purpose of this project is to create a working MySQL DAL to distance ourselves from the
menial tasks that are associated with PHP/MySQL systems, such as passing around
connection handles and iterating through result sets. We will create an object which will
create and maintain our connection to our database, provide us with the tools necessary to
perform all of our required SQL queries, properly handle errors, tidily present our data, and
keep our application code clean.
Defining the Object:
Our Database object will consist of local variables, a constructor, mutators, and methods. I
will leave out destructor and scoping from the database class, in order to maintain
compatibility with PHP4.
Local Variables:
We will need variables to keep track of our connection information, as well as keep track of
any open connections we may have.
The Constructor:
The Constructor for our class will server to create an instance of our Database class. It will
provide the class' variables with their initial values.
The Mutators:
Our class will provide the functionality for changing databases on the fly, therefor we will
design it to allow us to alter our connection information, both individually, and as a whole.
The Methods:
We are designing our DAL to connect to a database, therefor our Database class better
have a connect method! Along with connect method, it should have a way of disconnecting,
performing queries, and returning results.