Home arrow PHP arrow Page 6 - Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables

Spotting the differences: the new "DBIGenerator" class in practice - PHP

Welcome to part two of the series "Building Object-Oriented Database Interfaces in PHP." In the previous article, I offered complete coverage of the role that database interfaces play in Web applications, highlighting the immediate benefits of having a centralized mechanism for accessing, processing and verifying data, within an object-oriented environment. The "DBIGenerator" class I showed for demonstration purposes in the first article doesn't scale well in real applications. In this article, we will start solving its problems with a revamped version of the class.

TABLE OF CONTENTS:
  1. Building Object-Oriented Database Interfaces in PHP: Abstracting Database Tables
  2. Turning back time: a quick look at the older "DBIGenerator" class
  3. Working with multiple database interfaces: improving the "DBIGenerator" class
  4. Getting closer: a detailed look at the "generate()' method
  5. Updating and deleting a row: the "update()" and "delete()" methods
  6. Spotting the differences: the new "DBIGenerator" class in practice
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
August 17, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: