note 74511 added to language.oop5.decon
| From: | frederic dot barboteu at laposte dot net | Date: | Sun, 15 Apr 2007 02:11:44 +0000 |
| Subject: | note 74511 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125405@lists.php.net to get a copy of this message | ||
The manual says:
"Like constructors, parent destructors will not be called implicitly by the engine."
This is true ONLY when a __destruct() function has been defined by the child class.
If no __destruct() function exists in the child class, the parent's one will be implicitly
executed.
So be carefull if you have some ancestor executing a particular task in its __destruct() function,
an you plan its childs to execute it or not, wether you include "parent::__destruct()" or
not.
If you want the child not to execute its parent __destruct() function, you must ensure that it has
its own __destruct() function, even if empty. Then the parent's one will not be executed.
This can be verified with the following code:
<?php
#
class AncestorClass {
function __destruct() {
echo '<br />AncestorClass: destructing '.get_class($this);
}
}
#
class ParentDestructClass extends AncestorClass {
function __destruct() {
echo 'ParentDestructClass: destructing itself';
parent::__destruct();
}
}
#
class EmptyDestructClass extends AncestorClass {
function __destruct() {
echo 'EmptyDestructClass: destructing itself';
}
}
#
class NoDestructClass extends AncestorClass {
}
#---
echo '<hr>';
$p=new ParentDestructClass();
unset($p);
echo '<hr>';
$e=new EmptyDestructClass();
unset($e);
echo '<hr>';
$n=new NoDestructClass();
unset($n);
echo '<hr>';
?>
which displays:
---
ParentDestructClass: destructing itself
AncestorClass: destructing ParentDestructClass
---
EmptyDestructClass: destructing itself
---
AncestorClass: destructing NoDestructClass
---
----
Server IP: 195.221.21.36
Probable Submitter: 86.197.155.110
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/74511
Del: integrated -- https://master.php.net/note/delete/74511/integrated
Del: useless -- https://master.php.net/note/delete/74511/useless
Del: bad code -- https://master.php.net/note/delete/74511/bad+code
Del: spam -- https://master.php.net/note/delete/74511/spam
Del: non-english -- https://master.php.net/note/delete/74511/non-english
Del: in docs -- https://master.php.net/note/delete/74511/in+docs
Del: other reasons-- https://master.php.net/note/delete/74511
Reject -- https://master.php.net/note/reject/74511
Search -- https://master.php.net/manage/user-notes.php