phptutorials.cjb.net - Free PHP Tutorials and Advice
 

PHP Classes Tutorial 1 - Database Abstraction and Classes Tutorial - Page 1 of 7

Tutorial Aims

This tutorial explains the basics behind classes and objects and, step by step, develops a functional database abstraction layer.

Intended Audience

The tutorial is intended for users who wish to learn about classes and objects, and want an easier way to interact with the a database in their scripts. No advanced technical knowledge is assumed.

What is a database abstraction layer?

A database abstraction layer (dal) brings together many ways for interacting with a database, it is used as a layer between your PHP scripts and the database server, in this instance a MySQL server. Abstraction is simply simplification. Something that is complicated is made easy. The inner workings of the dal is irrelevent when using it, in this way it can be treated as a 'black box', you know the inputs, and you know outputs and you don't care how it gets there, except in this article we will be showing you how it works.

Why use a dal?

Portability
Should someone wish to run our code on a system with a database server other than MySQL, only the dal would need recoding for our scripts to work, without a dal every page with database interaction would need editing.
Technique
This will give experience in coding classes and database interaction and, hopefully, open your eyes to several useful MySQL commands that PHP has that you may not have previously been aware of.
Simplicity
Sometimes it can be hard to remember the standard PHP way of interacting with databases, just because everyone codes in their own style and it can be easier and simpler to code in your own way.
Reusability and Efficiency
Using centralised code for database interaction means every time you need to do something with a database you don't have to retype all the code, simply reuse your current code! This will save you coding time. Any updates or efficiency improvements you make will be reflected instantly across all your scripts.
Learn
Learn about Classes and Objects, and an example use of them.