PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Putting the MySQL abstraction layer to work
    ( Page 4 of 4 )

    As I stated in the section that you just read, the best way to understand how this MySQL abstraction layer can be used consists of developing an illustrative example that shows the real capacity offered by the bridge pattern.

    In response to this, below I coded a pair of short scripts. They first connect to MySQL using the respective "MySQL" class that you saw before, and then use the "mysqli" library for accessing the database server. Here are the corresponding code samples:

    // example using the 'MySQL' class try{ // instantiate 'BridgeMySQLConnector' class // connect to MySQL using 'MySQL' wrapping class $bmysqlcon=new BridgeMySQLConnector
    ('host','user','password','database','mysql'); // run query using 'MySQL' wrapping class     $bmysqlcon->query('SELECT * FROM users'); } catch(Exception $e){ echo $e->getMessage(); exit(); } try{ // example using the 'mysqli' extension // connect to MySQL using 'mysqli' library $bmysqlcon=new BridgeMySQLConnector
    ('host','user','password','database','mysqli'); // run query using 'mysqli' library    $bmysqlcon->query('SELECT * FROM users'); } catch(Exception $e){ echo $e->getMessage(); exit(); }

    See how easy it was to connect to MySQL using either the wrapping class or the "mysqli" extension? I bet you do! In both cases, I also executed a "SELECT" statement against a hypothetical "USERS" database table, but logically this condition can be easily modified to run other SQL commands.

    As you saw, learning how to apply the bridge design pattern with PHP was indeed an enjoyable experience, right?

    Final thoughts

    At this point, it's quite probable that you have learned the fundamentals of using the bridge design pattern with PHP 5. Personally, I consider this pattern to be very useful for decoupling a class from its implementation, if for some reason abstract classes are not a viable approach.

    Besides, its versatility allows you to apply it in a great variety of cases. Therefore I recommend that you keep it at hand if you want to achieve a high level of abstraction with your PHP-based applications. See you in the next PHP tutorial!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek