Specifications

We then call operations the same way that we call other functions: by using their name and
placing any parameters that they need in brackets. Because these operations belong to an
object rather than normal functions, we need to specify to which object they belong. The
object name is used in the same way as an objects attributes as follows:
$a->operation1();
$a->operation2(12, “test”);
If our operations return something, we can capture that return data as follows:
$x = $a->operation1();
$y = $a->operation2(12, “test”);
Implementing Inheritance in PHP
If our class is to be a subclass of another, you can use the extends keyword to specify this.
The following code creates a class named B that inherits from some previously defined class
named A.
class B extends A
{
var $attribute2;
function operation2()
{
}
}
If the class A was declared as follows:
class A
{
var $attribute1;
function operation1()
{
}
}
all the following accesses to operations and attributes of an object of type B would be valid:
$b = new B();
$b->operation1();
$b->attribute1 = 10;
$b->operation2();
$b->attribute2 = 10;
Note that because class B extends class A, we can refer to operation1() and $attribute1,
although these were declared in class A. As a subclass of A, B has all the same functionality and
data. In addition,
B has declared an attribute and an operation of its own.
Object-Oriented PHP
C
HAPTER 6
6
OBJECT-ORIENTED
PHP
155
08 7842 CH06 3/6/01 3:34 PM Page 155