#native_company# #native_desc#
#native_cta#

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

By PHP Builder Staff
on March 8, 2004

Option 2: Extra Queries for New Fields
Another option is to add num_mount_points to the hippos table as above, but change ArmedHippo
so it calls Hippo::load() to handle the id and name, and then deals with
num_mount_points itself. For example, we can implement
ArmedHippo::load() as:

<?

function load($id) {

    
parent::load($id);

    
$conn mysql_connect('localhost''hippo_user''hippo_user');

    
mysql_select_db('hippos');

    
$result mysql_query("select num_mount_points from hippos where id=$id");

    
$this->num_mount_points mysql_result($result0'num_mount_points');

    
application_utility_tasks();

}

?>



Line 2 calls Hippo::load() to get id and name from the database. The
method then opens the database, executes an SQL statement, and fetches num_mount_points.
How will this work when we add the weight property? Quite well, since changes to the hippos table
only affect Hippo’s methods. However, this approach reduces application performance.
ArmedHippo::load() calls Hippo::load(),
which open the database and runs an SQL statement. Then ArmedHippo::load()
connects to the database again, and runs another SQL statement. Add
StealthHippo, and things get worse. Caching and connection
pooling will reduce the performance hit, but not eliminate it. Further,
application_utility_tasks() is still called all over the place.
This illustrates a general problem with OOP. Classes hide implementation details, including
their use of resources. Sometimes this can adversely affect system performance. And since
you don’t normally look at all the code in existing classes when you extend them, you might
not know why performance has suddenly decreased.
Option 3: New Tables for Subclasses
A third option is to leave the hippos table as it is, and add a new table for the
ArmedHippo class:
Field Type Notes
id integer Primary key
Auto increment
num_mount_points integer  
The id field is a foreign key into the hippos table. For example, a record with an
id of 25 in the hippos table will also have an id of 25 in the new table.
This has the same disadvantages as option 2, and adds another all of its own. Decision
makers often want to explore data, looking for ways to improve production, gain market share,
reduce costs, etc. They want to use friendly tools to query databases, like Access and Excel.
Introducing extra tables and links makes their task more difficult. It’s not too bad for one
inherited class, but a real application might have a dozen of them.

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