PHP
  Home arrow PHP arrow Page 3 - Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects
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: Working with Multiple Data Access Objects
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2005-08-24


    Table of Contents:
  • Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects
  • Getting in touch with a DB Interface: A brief look at the "DBI Generator" class
  • Code generator in a nutshell: one class that generates another one
  • Some useful examples: a practical usage of the "DBIGenerator" class
  • Adding rows to both tables: working with multiple data access objects

  • 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: Working with Multiple Data Access Objects - Code generator in a nutshell: one class that generates another one
    (Page 3 of 5 )

    Once we've called the "generate()" method, one data access object $user has been programmatically created. But let's go one step further: if the object is available to be used, there must be a class from which the object was properly instantiated. That's the main task of this generator class, as you might guess. One DB Interface class has been generated "behind the scenes," providing a bridge between the database table and the application logic. As you've seen before, this is the class that I obtain as a result, after executing the above code:

    class User{
    var $id='';
    var $firstname='';
    var $lastname='';
    var $email='';
    function User(){}
    function setid($id){
    $this->id=$id;
    }
    function getid(){
    return $this->id;
    }
    function setfirstname($firstname){
    $this->firstname=$firstname;
    }
    function getfirstname(){
    return $this->firstname;
    }
    function setlastname($lastname){
    $this->lastname=$lastname;
    }
    function getlastname(){
    return $this->lastname;
    }
    function setemail($email){
    $this->email=$email;
    }
    function getemail(){
    return $this->email;
    }
    function load(){
    $r=mysql_query("SELECT * FROM users WHERE id='$this->id'");
    return mysql_fetch_array($r,MYSQL_ASSOC);
    }
    function submit(){
    mysql_query("INSERT INTO users SET firstname='$this-
    >firstname',lastname='$this->lastname',email='$this->email'");
    $this->id=mysql_insert_id();
    }
    function update(){
    mysql_query("UPDATE users SET firstname='$this-
    >firstname',lastname='$this->lastname',email='$this->email' WHERE
    id='$this->id'");
    }
    function delete(){
    mysql_query("DELETE FROM users WHERE id='$this->id'");
    }
    }

    Definitely, the "DBIGenerator" class does its thing building the corresponding database interface class for a specified database table. In this case, I'm showing a more readable version of the code generated, since in its obfuscated version, it doesn't contain any new lines. However, this is not a relevant issue for the PHP interpreter.

    Well, happily we've seen how a simple code generator works, because we have a class that is capable of generating one or more classes, according to the particular application requirements. Do you realize the actual power of this approach? I guess you do. However, let's implement some examples to see how a database interface can be put to work for us.



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

       

    PHP ARTICLES

    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...
    - Handling Attachments and Sending HTML Email ...
    - Sending Blind Carbon Copies with Swift Mailer
    - Using Swift Mailer's Cc MIME Header
    - Using Sendmail and Mail() with Swift Mailer


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code

     


     


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