#native_company# #native_desc#
#native_cta#

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

By PHP Builder Staff
on March 8, 2004

Let’s create a subclass of Hippo called ArmedHippo. Hippo has the attributes
id and name. ArmedHippo
inherits these attributes, and adds one of its own: number of mount points.

<?php

class ArmedHippo extends Hippo 

{    

    var 
$num_mount_points null;    

    
//Load record from DB    

    
function load($id

    {        

        ...    

    }    

    
//Save record to DB    

    
function save() 

    {       

         ...    

    }

}

?>



The next step is to write ArmedHippo::load() and
ArmedHippo::save(). It’s in data storage methods in subclasses
like this that object-relational mapping becomes a problem. If we’re not cautious, we can
make the OOP code hard to maintain. Further, unless we carefully prepare the groundwork now,
future application updates will make things even worse.
We’re going to look at several ways of doing the same three things:
  • Implementing ArmedHippo::load() and
    ArmedHippo::save().
  • Recording the weight of each hippo. The simplest thing to do in PHP 4 is to change
    the Hippo class, adding a new property. But can we do that without affecting
    ArmedHippo?
  • Recording data about stealth hippos. Stealth hippos are armed hippos painted black,
    and equipped with olfactory detection suppression systems (ODSS, military jargon for
    deodorant). The class StealthHippo will extend ArmedHippo.
    Can we add it without affecting other classes?
The best case is that each change won’t disturb existing code. The more code that is
disturbed, the more costly it will be to maintain the application.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|