The Employee constructor could still be invoked within Manager??™s
constructor, like this:
Employee::__construct()
Calling the Employee constructor like this results in the same outcome as that shown in
the example.
?– Note You may be wondering why the extremely useful constructor-overloading feature, available in
many OOP languages, has not been discussed. The answer is simple: PHP does not support this feature.
Destructors
Although objects were automatically destroyed upon script completion in PHP 4, it
wasn??™t possible to customize this cleanup process. With the introduction of destructors
in PHP 5, this constraint is no more. Destructors are created like any other method
but must be titled __destruct(). An example follows:
class Book
{
private $title;
private $isbn;
private $copies;
function __construct($isbn)
{
echo "
Book class instance created.
";
}
CHAPTER 6 ?– O BJECT-ORIENTED PHP 187
function __destruct()
{
echo "
Book class instance destroyed.
";
}
}
$book = new Book("1893115852");
?>
Here??™s the result:
Book class instance created.
Book class instance destroyed.
When the script is complete, PHP will destroy any objects that reside in memory.
Therefore, if the instantiated class and any information created as a result of the
instantiation reside in memory, you??™re not required to explicitly declare a destructor.
Pages:
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265