Home arrow PHP arrow Page 3 - Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5

Creating a bridge class with PHP 5 - 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.

TABLE OF CONTENTS:
  1. Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
  2. Creating the first abstraction layer for MySQL
  3. Creating a bridge class with PHP 5
  4. Putting the MySQL abstraction layer to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
January 17, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Building a bridge class that allows you to connect to MySQL by using either the traditional "mysql" library or the "mysqli" extension is a straightforward task that can be accomplished with minor effort.

In the prior section I defined a pair of MySQL wrapping classes aimed at accessing MySQL via a conventional approach. Therefor, the process for creating a bridge class is reduced to coding the mentioned class in such a way that it can be instructed programmatically of the type of interface to be used for accessing the database server.

Does this sound rather confusing to you? It really isn't, particularly if you take a look at the signature that corresponds to the class below:

// define 'BridgeMySQLConnector' class class BridgeMySQLConnector{ private $conInterface; private $db; public function __construct($host,$user,$password,$database,
$conInterface){        if($conInterface!='mysql'&&$conInterface!='mysqli'){ throw new Exception
('Invalid connection interface for MySQL');     }        if($conInterface=='mysql'){ $this->db=new MySQL($host,$user,$password,
$database); } else{ $this->db=new mysqli($host,$user,$password,
$database); } } public function query($query){ return $this->db->query($query);    } }

As you can see, the "BridgeMySQLConnector" class coded above accepts the typical set of parameters required for connecting to MySQL, that is the hostname, the user/password combination and the selected database, as well as the interface to be used for performing this process.

In addition, you should realize that the previous class strictly follows the model dictated by the bridge pattern. It will use the library passed in as a parameter for accessing the database server, but in all cases there's no specific implementation concerning how this task must be carried out.

Thus, what does all this mean? Yes, you guessed right! The logic required for connecting to MySQL has been completely decoupled from the class itself, since this procedure is tackled by the corresponding "bridged" objects, in this case referenced inside the class as "MySQL" and "mysqli" respectively. Building a bridge class with PHP 5 is actually easy after all, isn't it?

Well, now that you know how the prior "BridgeMySQLConnector" class works, it would be very instructive to develop a practical example where all the previous classes are used in conjunction. In this way, you'll see more clearly how this abstraction layer can be utilized for accessing the MySQL server.

As usual, if you want to learn how the layer in question will be put to work, go ahead and read the following section. I'll be there, waiting for you.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: