Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using PHP Objects to Access Your Database Tables (Part 2)

By Tony Marston
2005-04-07


Custom Processing

It is all very well having all this standard code to perform standard processing, but how do you deal with those situations where you need more than the standard processing? The golden rule here is that the standard code should be left unchanged, so what we need are a series of empty spaces where custom code can be inserted as and when necessary. What I mean by an 'empty space' is a method/function which is pre-defined in the base class but which is empty. The standard code needs to contain calls to each of these custom methods/functions at an appropriate point so that they can be actioned even if they are empty. This is best demonstrated by showing you some sample code.

For example, here is a version of my 'getData' method which contains calls to custom functions:

function getData ($where)

{
$this->errors = array();
$this->data_array = array();

// perform any custom pre-retrieve processing
$where = $this->pre_getData($where);

if (empty($this->errors))
// retrieve the data
$this->data_array = $this->dml_getData($where);
} // if

if (empty($this->errors))
// perform any custom post-retrieve processing
$this->data_array = $this->post_getData($this->data_array, $where);
} // if

return $this->data_array;

} // getData
The standard function 'dml_getdata' is the one which communicates with the physical database. The custom functions, in this example 'pre_getData' and 'post_getData', are created along these lines:

function pre_getData ($where)

// perform custom processing before database record(s) are retrieved.
{
// custom code goes here

return $where;

} // pre_getData
When an actual table class (subclass) is created as an extension of the base class (superclass) it will inherit these empty functions unless alternatives are defined within the subclass. By incorporating a separate 'pre' and 'post' function for each of the major functions in the base class I have solved two potential problems:

It makes it easy for the developer to know where to insert any custom code for each of the different events.
It greatly reduces the possibility of accidentally corrupting any of the standard code as these dummy functions do not have any default code of their own.

Tutorial Pages:
» Intended Audience
» Extending the MySQL SELECT statement
» Adding the features of a Data Dictionary/Repository
» Using the Data Dictionary/Repository
» Changing Candidate Keys
» Deleting Rows
» Custom Processing
» Virtual Database Tables
» Summary


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1