Specifications
Declaring B does not affect the original definition of A. Consider the following two lines of
code:
$a = new A();
$a -> operation();
We have created an object of type A and called its operation() function. This will produce
Something
The value of $attribute is default value
proving that creating B has not altered A. If we create an object of type B, we will get different
output.
This code
$b = new B();
$b -> operation();
will produce
Something else
The value of $attribute is different value
In the same way that providing new attributes or operations in a subclass does not affect the
superclass, overriding attributes or operations in a subclass does not affect the superclass.
A subclass will inherit all the attributes and operations of its superclass, unless you provide
replacements. If you provide a replacement definition, this takes precedence and overrides the
original definition.
Unlike some other OO languages, PHP does not allow you to override a function and still be
able to call the version defined in the parent.
Inheritance can be many layers deep. We can declare a class imaginatively called C, that
extends B and therefore inherits features from B and from B’s parent A. The class C can again
choose which attributes and operations from its parents to override and replace.
Multiple Inheritance
Some OO languages support multiple inheritance, but PHP does not. This means that each
class can only inherit from one parent. No restrictions exist for how many children can share a
single parent.
It might not seem immediately clear what this means. Figure 6.1 shows three different ways
that three classes named A, B, and C can inherit.
Object-Oriented PHP
C
HAPTER 6
6
OBJECT-ORIENTED
PHP
157
08 7842 CH06 3/6/01 3:34 PM Page 157