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.
class ArmedHippo extends Hippo
var $num_mount_points = null;
//Load record from DB
//Save record to DB
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
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
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.