HomePHP Page 2 - Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
Creating the first abstraction layer for MySQL - PHP
If learning how to incorporate creational design patterns into your own PHP 5 applications is a high-priority topic for you, then this set of comprehensive articles might be what you’re looking for. Welcome to the concluding installment of the series “Using Bridge Classes with PHP 5.” These tutorials will teach you how to implement the bridge pattern in PHP by providing you with many hands-on examples that can be included in your existing and future web projects.
As I explained in the introduction, my intention here is to develop a simple abstraction layer by using only a bridge class and a couple of "bridged" objects. The purpose of doing this is basically to have access to the MySQL server by utilizing either the conventional "mysql" extension, or the highly-improved "mysqli" library that comes bundled with PHP 5.
Having explained how I plan to use the bridge design pattern in this specific case, let me show you the first class involved in this example. It consists of a simple wrapper for MySQL, and logically uses the respective "mysql" extension as well. Here is the signature for this brand new class. Please have a look at it:
// define 'MySQL' class
class MySQL{
public function __construct($host,$user,$password,$database){
if(!$conId=mysql_connect($host,$user,$password)){
throw new Exception('Error connecting to
the server');
}
if(!mysql_select_db($database,$conId)){
throw new Exception('Error selecting
database');
}
}
// run query
public function query($query){
if(!$this->result=mysql_query($query)){
throw new Exception('Error performing query '.$query);
}
return new Result($this,$this->result);
}
}
// define 'Result' class
class Result {
private $mysql;
private $result;
public function __construct(&$mysql,$result){
$this->mysql=&$mysql;
$this->result=$result;
}
// fetch row
public function fetch_array(){
return mysql_fetch_assoc($this->result);
}
// count rows
public function countRows(){
if(!$rows=mysql_num_rows($this->result)){
throw new Exception('Error counting rows');
}
return $rows;
}
// count affected rows
public function countAffectedRows(){
if(!$rows=mysql_affected_rows($this->mysql->conId)){
throw new Exception('Error counting affected
rows');
}
return $rows;
}
// get ID form last-inserted row
public function getInsertID(){
if(!$id=mysql_insert_id($this->mysql->conId)){
throw new Exception('Error getting ID');
}
return $id;
}
// seek row
public function seekRow($row=0){
if(!is_int($row)||$row<0){
throw new Exception('Invalid result set
offset');
}
if(!mysql_data_seek($this->result,$row)){
throw new Exception('Error seeking data');
}
}
}
As you can see, there's nothing special with reference to the MySQL wrapping classes listed above. Similar to many abstraction classes that you have possibly used in many web applications, these allow you to perform some simple operations, such as connecting to the server, selecting a specific database, running queries, and processing result sets in a few useful ways.
However, the pair of MySQL wrapping classes that you just saw definitely aren't the main topic of this article. I defined them here only with the purpose of creating another application that lets you access the MySQL server, either with the traditional "mysql" extension, or the improved "mysqli" library.
In this case, the first requirement has been already satisfied, since the previous classes use the conventional extension. Therefore, it's time to move on and see how to develop a bridge class which will allow us to use MySQL by utilizing the two mentioned libraries.
Things are getting really exciting now! Therefore, if you want to learn how this bridge class will be created, please keep reading.