Specifications
It is important to note that inheritance only works in one direction. The subclass or child inher-
its features from its parent or superclass, but the parent does not take on features of the child.
This means that the last two lines in this code are wrong:
$a = new A();
$a->operation1();
$a->attribute1 = 10;
$a->operation2();
$a->attribute2 = 10;
The class A does not have an operation2() or an attribute2.
Overriding
We have shown a subclass declaring new attributes and operations. It is also valid and some-
times useful to redeclare the same attributes and operations. We might do this to give an
attribute in the subclass a different default value to the same attribute in its superclass, or to
give an operation in the subclass different functionality to the same operation in its superclass.
This is called overriding.
For instance, if we have a class A:
class A
{
var $attribute = “default value”;
function operation()
{
echo “Something<br>”;
echo “The value of \$attribute is $this->attribute<br>”;
}
}
and want to alter the default value of $attribute and provide new functionality for opera-
tion(), we can create the following class B, which overrides $attribute and operation():
class B extends A
{
var $attribute = “different value”;
function operation()
{
echo “Something else<br>”;
echo “The value of \$attribute is $this->attribute<br>”;
}
}
Using PHP
P
ART I
156
08 7842 CH06 3/6/01 3:34 PM Page 156