PHP
  Home arrow PHP arrow Page 4 - 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 - Getting closer: a detailed look at the "generate()' method
    ( Page 4 of 6 )

    This method is the most resource intensive, since it dynamically generates the class file that will interface with a database table. In general terms, the class file is built by concatenating strings, so the generation process shouldn't present any difficulties to being understood.

    First, the method obtains the field names of the table passed to the class, assigning them directly as class properties, storing temporarily the output in the $methods variable, to be included at a later time. The lines below perform these operations:

    // build class header

    $str='<?php class '.$this->name.'{';

    if(!$result=mysql_query("SHOW COLUMNS FROM $this->table")){

    die('Could not run query '.mysql_error());

    }

    // build data member declaration

    if(mysql_num_rows($result)>0){

    while($row=mysql_fetch_array($result,MYSQL_ASSOC)){

    $str.='var $'.$row['Field'].'=\'\';';

    $methods.='function set'.$row['Field'].'($'.$row['Field'].')
    {$this->'.$row['Field'].'=$'.$row['Field'].';}';

    $methods.='function get'.$row['Field'].'(){return $this-
    >'.$row['Field'].';}';

    // store field names in array

    $fields[]=$row['Field'];

    }

    }

    Then, the class constructor is generated with this single line:

    $str.='function '.$this->name.'(){}';

    Finally, the accessors and modifiers methods are appended to the output, like this:

    // build modifiers and accessors

    $str.=$methods;

    The next step is to append the "load()" and "submit()" methods to the output string, accomplishing the "SELECT" and "INSERT" single-row operations. They're defined as listed below:

    // build load() method

    $str.='function load(){$r=mysql_query("SELECT * FROM
    '.$this->table.' WHERE id=\'$this->id\'");';

    $str.='return mysql_fetch_array($r,MYSQL_ASSOC);}';

    // build submit() method

    $str.='function submit(){mysql_query("INSERT INTO '.$this-
    >table.' SET ';

    foreach($fields as $field){

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

    }

    $str.='");$this->id=mysql_insert_id();';

    $str=preg_replace("/,\"/","\"",$str).'}';

    Notice that in this case, we're using the native MySQL functions to execute queries and retrieve a row. However, this condition might be easily changed by utilizing methods provided by another object.

    So far, so good. We've already described the two main methods used to retrieve and insert a row into the database table. Our next task is to explain the remaining methods, "update()" and "delete()". Don't you worry, because they're just a few lines away.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek