PHPBuilder - A Practical Approach to Object-Relational Mapping in PHP Page 8



RSS Twitter
Articles Application Architecture

A Practical Approach to Object-Relational Mapping in PHP - Page 8

by: PHP Builder Staff
|
March 8, 2004

The code for ArmedHippo is quite simple:

<?php
class ArmedHippo extends Hippo 
{
    var 
$num_mount_points;
    
//Constructor
    
function ArmedHippo() 
    {
        
parent::Hippo();
        
$this->addField('num_mount_points',
        new 
DataField(DataField::TYPE_NUMERIC(), true) );
    }
    
//Load record from DB
    
function load($id
    {
        
parent::load($id);
        
$this->num_mount_points $this->getDBValue('num_mount_points');
    }
    
//Save record to DB
    
function save() 
    {
        
$this->setDBValue('num_mount_points'$this->num_mount_points );
        
parent::save();
    }
}
?>
The constructor calls Hippo's constructor (line 5), then adds a new data field, num_mount_points (line 6). ArmedHippo::load() calls Hippo::load() (line 11), then extracts the value for the field it manages (line 12). ArmedHippo::save() stores the value for num_mount_points (line 16), then calls Hippo::save() (line 17).
Now we've got what we wanted. ArmedHippo can be added without disturbing Hippo. A weight property can be added to Hippo, without requiring changes to ArmedHippo. StealthHippo can inherit from ArmedHippo, with no changes to existing code. Further, no calls to functions that perform application utility tasks appear in Hippo, ArmedHippo, or StealthHippo. They are centralized in BusinessBase.
BusinessBase starts like this:

<?php
class BusinessBase 
{
    var 
$table_name null;
    var 
$record null;
    var 
$_id_field null;
}
?>
$table_name is, of course, the name of the RDB table for the class. $record is an associative array describing each record in the table. The array's index is the name of a field in the table (e. g., id). The value in each array element is a DataField object:

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Comment:



Comment:

(Maximum characters: 1200). You have characters left.