Option 2: Extra Queries for New Fields
Another option is to add num_mount_points to the hippos table as above, but change
so it calls
ArmedHippo
so it calls
Hippo::load()
to handle the id and name, and then deals withnum_mount_points
itself. For example, we can implementArmedHippo::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($result, 0, 'num_mount_points');
application_utility_tasks();
}
?>
Line 2 calls
method then opens the database, executes an SQL statement, and fetches num_mount_points.
Hippo::load()
to get id and name from the database. Themethod 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.
which open the database and runs an SQL statement. Then
connects to the database again, and runs another SQL statement. Add
pooling will reduce the performance hit, but not eliminate it. Further,
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 connectionpooling 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.
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.
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.
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.