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

Updating and deleting a row: the "update()" and "delete()" methods - 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.

  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



Not surprisingly, these two methods are self-explanatory. They accomplish the tasks of updating and deleting a row from a table, using regular DML statements. Their definitions look like this:

// build update() method

$str.='function update(){mysql_query("UPDATE '.$this-
>table.' SET ';

foreach($fields as $field){




$str.=' WHERE id=\'$this->id\'");}';

// build delete() method

$str.='function delete(){mysql_query("DELETE FROM '.$this-
>table.' WHERE id=\'$this->id\'");}';

The above lines append the methods to the progressive output, for providing the DB interface class with the ability to update and delete a row. As you might guess, building the class is just a matter of concatenating simple strings.

Once the complete class code has being generated, we need to write it to a file, so the class file will be automatically created for spawning data access objects, and tying these objects to each related database table. The lines below create the specified class file at the given path:

// write class code to file

$fp=fopen($this->path.$this->name.'.php','w') or die('Failed
opening file');



// delete temporary variables


Lastly, in order to make the class behave as an "object factory", we define the "getObject()" method, which includes the DB interface file and returns a new object for usage within the application:

function getObject(){

// check if class file exists



// create object

return new $this->name;


return false;


Although the class definition is certainly quite simple, it reveals its real strength when used in conjunction with multiple DB interfaces, allowing more flexible and efficient database table manipulation.

However, by now we'll see how the class can be implemented in a rather basic way, so we're able to spot the main differences compared to the version presented in the previous article. Thus, in the next lines, we'll see the a brief example for proper class usage.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: