PHP
  Home arrow PHP arrow Page 3 - Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-01-17


    Table of Contents:
  • Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
  • Creating the first abstraction layer for MySQL
  • Creating a bridge class with PHP 5
  • Putting the MySQL abstraction layer to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5 - Creating a bridge class with PHP 5
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT