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

Putting the MySQL abstraction layer to work - 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

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
 

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: