PHP
  Home arrow PHP arrow Page 4 - Abstracting Database Access Using Poly...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Abstracting Database Access Using Polymorphism with Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this first article of the series, the concept of Polymorphism is used to create a...
       · I think you made a conceptual mistake in your exemple...You should not have...
       · Hello Fred,Thank you for your posting your comments. However, mi article doesn’t...
       · I've read almost all your articles regarding php and oop.This one is by far the...
       · I'm going to disagree with you both here. First, abstract classes also lead to...
       · Thank you for your compliments on my PHP article. I indeed appreciate them, and also...
       · Hello Pim,First off, thank you for posting your comments here, since I believe...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway