PHP
  Home arrow PHP arrow Page 5 - Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables
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

Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2005-08-17


    Table of Contents:
  • Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables
  • Turning back time: a quick look at the older "DBIGenerator" class
  • Working with multiple database interfaces: improving the "DBIGenerator" class
  • Getting closer: a detailed look at the "generate()' method
  • Updating and deleting a row: the "update()" and "delete()" methods
  • Spotting the differences: the new "DBIGenerator" class in practice

  • 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


    Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables - Updating and deleting a row: the "update()" and "delete()" methods
    ( Page 5 of 6 )

    Not surprisingly, these two methods are self-explanatory. They accomplish the tasks of updating and deleting a row from a table, using regular DML statements. Their definitions look like this:

    // build update() method

    $str.='function update(){mysql_query("UPDATE '.$this-
    >table.' SET ';

    foreach($fields as $field){

    $str.=($field!='id')?$field.'=\'$this->'.$field.'\',':'';

    }

    $str=preg_replace("/,$/","",$str);

    $str.=' WHERE id=\'$this->id\'");}';

    // build delete() method

    $str.='function delete(){mysql_query("DELETE FROM '.$this-
    >table.' WHERE id=\'$this->id\'");}';

    The above lines append the methods to the progressive output, for providing the DB interface class with the ability to update and delete a row. As you might guess, building the class is just a matter of concatenating simple strings.

    Once the complete class code has being generated, we need to write it to a file, so the class file will be automatically created for spawning data access objects, and tying these objects to each related database table. The lines below create the specified class file at the given path:

    // write class code to file

    $fp=fopen($this->path.$this->name.'.php','w') or die('Failed
    opening file');

    fwrite($fp,$str);

    fclose($fp);

    // delete temporary variables

    unset($fp,$str,$row,$fields,$field);

    Lastly, in order to make the class behave as an "object factory", we define the "getObject()" method, which includes the DB interface file and returns a new object for usage within the application:

    function getObject(){

    // check if class file exists

    if(file_exists($this->path.$this->name.'.php')){

    require_once($this->path.$this->name.'.php');

    // create object

    return new $this->name;

    }

    return false;

    }

    Although the class definition is certainly quite simple, it reveals its real strength when used in conjunction with multiple DB interfaces, allowing more flexible and efficient database table manipulation.

    However, by now we'll see how the class can be implemented in a rather basic way, so we're able to spot the main differences compared to the version presented in the previous article. Thus, in the next lines, we'll see the a brief example for proper class usage.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...





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