I'm selling company assets to finance my yacht!
Because all employees have a name, the Executive class inherits from the Employee
class, saving you the hassle of having to re-create the name member and the corresponding
getter and setter. You can then focus solely on those characteristics that
are specific to an executive, in this case a method named pillageCompany(). This
method is available solely to objects of type Executive, and not to the Employee class
or any other class, unless of course you create a class that inherits from Executive.
The following example demonstrates that concept, producing a class titled CEO,
which inherits from Executive:
class Employee {
...
}
class Executive extends Employee {
...
}
CHAPTER 7 ?– ADVANCED OOP FEATURES 201
class CEO extends Executive {
function getFacelift() {
echo "nip nip tuck tuck";
}
}
$ceo = new CEO();
$ceo->setName("Bernie");
$ceo->pillageCompany();
$ceo->getFacelift();
?>
Because Executive has inherited from Employee, objects of type CEO also have
all the members and methods that are available to Executive, in addition to the
getFacelift() method, which is reserved solely for objects of type CEO.
Inheritance and Constructors
A common question pertinent to class inheritance has to do with the use of constructors.
Does a parent class constructor execute when a child is instantiated? If so, what
happens if the child class also has its own constructor? Does it execute in addition to
the parent constructor, or does it override the parent? Such questions are answered
in this section.
Pages:
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279