Specifications

this would rarely be a problem. Bicycles are not likely to get confused and start using a cars
move operation instead. However, a programming language does not possess the common
sense of the real world, so the language must support polymorphism in order to know which
move operation to use on a particular object.
Polymorphism is more a characteristic of behaviors than it is of objects. In PHP, only member
functions of a class can be polymorphic. A real world comparison is that of verbs in natural
languages, which are equivalent to member functions. Consider the ways a bicycle can be used
in real life. You can clean it, move it, disassemble it, repair it, or paint it, among other things.
These verbs describe generic actions because you dont know what kind of object is being
acted on. (This type of abstraction of objects and actions is one of the distinguishing character-
istics of human intelligence.)
For example, moving a bicycle requires completely different actions from those required for
moving a car, even though the concepts are similar. The verb move can be associated with a
particular set of actions only once the object acted on is made known.
Inheritance
Inheritance allows us to create a hierarchical relationship between classes using subclasses. A
subclass inherits attributes and operations from its superclass. For example, car and bicycle
have some things in common. We could use a class vehicle to contain the things such as a
color attribute and a move operation that all vehicles have, and then let our car and bicycle
classes inherit from vehicle.
With inheritance, you can build on and add to existing classes. From a simple base class, you
can derive more complex and specialized classes as the need arises. This makes your code
more reusable, which is one of the important advantages of an object-oriented approach.
Using inheritance might save us work if operations can be written once in a superclass rather
than many times in separate subclasses. It might also allow us to more accurately model real-
world relationships. If a sentence about two classes makes sense with is a between the
classes, inheritance is probably appropriate. The sentence a car is a vehiclemakes sense, but
the sentence a vehicle is a cardoes not make sense because not all vehicles are cars.
Therefore, car can inherit from vehicle.
Creating Classes, Attributes, Operations in PHP
So far, we have discussed classes in a fairly abstract way. When creating a class in PHP, you
must use the keyword class.
Using PHP
P
ART I
150
08 7842 CH06 3/6/01 3:34 PM Page 150