PHP
  Home arrow PHP arrow Page 6 - 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? 
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 - Spotting the differences: the new "DBIGenerator" class in practice
    ( Page 6 of 6 )

    Probably, one of the most illustrative ways to demonstrate how the class "fits" into an application is by coding an example. Therefore, here's a possible implementation of the class:

    require_once 'mysqlclass.php';

    require_once 'dbigeneratorclass.php';

    // instantiate a new MySQLConnector object

    $db=&new MySQLConnector(array
    ('host'=>'host','user'=>'user','password'=>
    'password','database'=>'database'));

    // instantiate a new DBIGenerator object

    $g=&new DBIGenerator('users','User','DBICLASSES/');

    // generate class file

    $g->generate();

    // get an user object

    if(!$user=$g->getObject()){

    die('Failed to create object');

    }

    In the above example, we included the class files, instantiating a $db "MySQLConnector" object to provide the necessary database connectivity. Then, we instantiated a "DBIGenerator" object, passing to it the name of the database table to be associated with, in this case "users"; the name of the DBI class to be created, "User"; and lastly, the path where the class file will be located, that is "DBICLASSES/".

    For this example, the "users" table contains the following fields: "id","firstname", "lastname" and "email", which implies that the class tied to it should represent these fields as properties, as well as having the corresponding setters and getters.

    After executing the snippet, a class file named "User.php" is created in the "DBICLASSES/" directory, and a "User" class has been dynamically generated, with the following definition:

    <?php 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'");}}?>

    Don't get confused about the code! The class has been simply created with no new lines on it. Since the PHP interpreter doesn't care about this, the file size is significantly reduced. However, to have a more readable version of the class, I show the same code, this time including the new lines:

    <?php

    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'");

    }

    }

    ?>

    Wasn't that good? I'm sure you'll agreed. The above class is a logical representation of the "users" table. Having such a useful class, we're able to perform all of the single-row DML operations directly on the table. Let's consider the power of this approach when we combine these classes to access multiple tables. It's really worth giving a try. That's what we will do in the next article!

    Wrapping up

    A few considerations are needed here. Over this part of the series, I've coded a new "DBIGenerator" class that presents greater flexibility to work with any database table supplied as class parameter. Also, hopefully I've demonstrated how powerful this class can be for accessing data by using a single communication point.

    However, the real strength of this approach is clearly evidenced when working with many classes tied to multiple tables. That's the pending task for the next article, so you don't have any excuses to miss it. See you soon!



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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT