Modify the CEO constructor like so:
function __construct($name) {
parent::__construct($name);
echo "
CEO object created!
";
}
Again instantiating the CEO class and executing getName() in the same fashion as
before, this time you??™ll see a different outcome:
CEO object created!
My name is Dennis
CHAPTER 7 ?– ADVANCED OOP FEATURES 203
You should understand that when parent::__construct() was encountered, PHP
began a search upward through the parent classes for an appropriate constructor.
Because it did not find one in Executive, it continued the search up to the Employee
class, at which point it located an appropriate constructor. If PHP had located a
constructor in the Employee class, then it would have fired. If you want both the Employee
and Executive constructors to fire, you need to place a call to parent::__construct() in
the Executive constructor.
You also have the option to reference parent constructors in another fashion. For
example, suppose that both the Employee and Executive constructors should execute
when a new CEO object is created. As mentioned in the last chapter, these constructors
can be referenced explicitly within the CEO constructor like so:
function __construct($name) {
Employee::__construct($name);
Executive::__construct();
echo "
CEO object created!
";
}
Interfaces
An interface defines a general specification for implementing a particular service,
declaring the required functions and constants without specifying exactly how it
must be implemented.
Pages:
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281