note 93387 deleted from language.oop.constructor by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 18:56:54 +0000 |
| Subject: | note 93387 deleted from language.oop.constructor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162518@lists.php.net to get a copy of this message | ||
Note Submitter: olksy
----
I previously used C++ and noted, that PHP does not invoke parent class destructor automatically, but
must be called explicitly from derived __destruct().
To illustrate, the next code
<?php
/*
* test object __destruct bevahior
*/
class TestDtrA {
public function __construct() {
echo __CLASS__ . " created\n";
}
public function __destruct() {
echo __CLASS__ . " destructor called\n";
}
}
class TestDtrB extends TestDtrA {
public function __construct() {
echo __CLASS__ . " created\n";
}
public function __destruct() {
// parent::__destruct(); <<explicit parent call
echo __CLASS__ . " destructor called\n";
}
}
function foo() {
echo __FUNCTION__ . ": in\n";
$instance = new TestDtrB();
echo __FUNCTION__ . ": out\n";
}
function bar() {
echo __FUNCTION__ . ": in\n";
echo __FUNCTION__ . ": out\n";
}
foo();
bar();
?>
will output
foo: in
TestDtrB created
foo: out
TestDtrB destructor called
bar: in
bar: out
(PHP 5.2.6)