Home arrow PHP arrow Page 3 - Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects

Code generator in a nutshell: one class that generates another one - PHP

In previous articles in this series, we examined the concept of database interfaces, and saw source code for a "DBIGenerator" class. Working with multiple DB interfaces permits the accomplishment of several operations on many tables at the same time. Alejandro Gervasio demonstrates the power of this concept, with examples.

TABLE OF CONTENTS:
  1. Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects
  2. Getting in touch with a DB Interface: A brief look at the "DBI Generator" class
  3. Code generator in a nutshell: one class that generates another one
  4. Some useful examples: a practical usage of the "DBIGenerator" class
  5. Adding rows to both tables: working with multiple data access objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
August 24, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: