PHP
  Home arrow PHP arrow Page 4 - Abstracting Database Access Using Polymorphism with Objects 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

Abstracting Database Access Using Polymorphism with Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2007-03-21


    Table of Contents:
  • Abstracting Database Access Using Polymorphism with Objects in PHP 5
  • What shouldn't be done when accessing distinct database systems
  • Using Polymorphism to create a database abstraction layer
  • Demonstrating the functionality of Polymorphism

  • 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


    Abstracting Database Access Using Polymorphism with Objects in PHP 5 - Demonstrating the functionality of Polymorphism
    ( Page 4 of 4 )

    In this section I'm going to create a couple of hands-on examples, which hopefully will help you understand more easily the convenience of working with polymorphic objects.

    This being said, here is the first code sample. It demonstrates how easy it is to interact with MySQL using the database abstraction layer that I developed earlier. The corresponding code listing is as follows:

    (example using MySQL database server)
    try{
      
    // use Factory pattern and Polymorphism to work with MySQL
      
    $factoryInstance=new DBFactory();
      
    $db=$factoryInstance->createDB
    ('MySQL','host','user','password','database');
      
    $db->query('SELECT name,email FROM users');
      
    while($row=$db->fetchRow()){
        
    echo $row['name'].' '.$row['email'].'<br />';
      
    }
      
    echo 'Number of rows returned by the query :'.$db->countRows
    ();
      
    /* displays the following
      
    user1 user1@domain.com
      
    user2 user2@domain.com
      
    user3 user3@domain.com
      
    user4 user4@domain.com
      
    user5 user5@domain.com
       user6 user6@domain.com
      
    user7 user7@domain.com
      
    user8 user8@domain.com
      
    user9 user9@domain.com
      
    user10 user10@domain.com
       Number of rows returned by the query :10

       */
    }
    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }

    As shown above, accessing the MySQL database server is extremely simple by using Polymorphism. The previous example uses the "query()", fetchRow()" and "countRows()" methods to perform some common tasks associated with MySQL, such as running queries and fetching/counting rows. But this advantage is even more clear if you look at the following example. It uses the same methods, but this time accesses the SQLite database system.

    Assuming that a sample SQLite database has been populated with basic data, the respective code listing is as follows:

    (example using SQLite database system)

    try{
      
    // use Factory pattern and Polymorphism to work with SQLite
      
    $factoryInstance=new DBFactory();
      
    $db=$factoryInstance->createDB('SQLite');
      
    $db->query('SELECT name,email FROM users');
      
    while($row=$db->fetchRow()){
        
    echo $row['name'].' '.$row['email'].'<br />';
      
    }
      
    echo 'Number of rows returned by the query :'.$db->countRows
    ();

       /* displays the following:

       User1 user1@domain.com
      
    User2 user2@domain.com
      
    User3 user3@domain.com
       Number of rows returned by the query :3

       */

    }
    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }

    Definitely, after examining the previous example, you'll have to agree with me that building a database abstraction layer using polymorphic objects is by far a much better approach than the one shown in the beginning of this tutorial.

    Naturally, I'm not saying here that you have to reinvent the wheel and write your own abstraction layers; there are many excellent packages available on the web. But I do want you to understand the advantages of using Polymorphism when building your PHP applications, since the concept can be rapidly extended to other areas.

    Final thoughts

    That's all for the moment. In this first article of the series, I demonstrated in a friendly way how to use polymorphic classes to build a simple database abstraction layer. Nevertheless, this is only the beginning, since in the next part I'm going to teach you how to use polymorphism to create dynamic web pages. You won't want to miss it!



     
     
    >>> 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