note 81458 added to language.oop5.decon
| From: | ashnazg@php.net | Date: | Thu, 28 Feb 2008 22:39:34 +0000 |
| Subject: | note 81458 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136579@lists.php.net to get a copy of this message | ||
While experimenting with destructs and unsets in relation to memory usage, I found what seems to be
one useful way to predict when __destruct() gets called... call it manually yourself.
I had previously assumed that explicitly calling unset($foo) would cause $foo->__destruct() to
run implicitly, but that was not the behavior I saw (php 5.2.5). The destructors weren't
running until the script ended, even though I was calling unset($foo) in the middle of my script.
So, having $foo->__destruct() unset all of $foo's component objects was not helping my
memory usage since my explicit unset($foo) was _not_ triggering $foo->__destruct()'s cleanup
steps.
Interestingly, what _did_ appear to happen is that calling unset($bar) from inside a destructor like
$foo->__destruct() _DID_ cause $bar->__destruct() to be implicitly executed. Perhaps this is
because $bar has a "parent" reference of $foo whereas $foo does not, and the object
destruction behaves differently... I don't know.
Lastly, even after explicitly calling $foo->__destruct() (even when it had
"unset($this);" inside it), the reference to the $foo object remained visible. I had to
still explicitly call unset($foo) to get rid of it.
So, my advice based on the behavior I saw in my experiments:
- always unset($bar) your class's component objects from inside that class's __destruct()
method; no explicit component destructor calls seem to be required other than those unsets, though.
- always explicitly call $foo->__destruct() in your code that _uses_ your class
- always explicitly follow $foo->__destruct() with unset($foo).
This seems to be the best cleanup approach to take. Just for my own sanity, I also will always keep
an unset($this) at the end of each __destruct() method.
----
Server IP: 69.147.83.197
Probable Submitter: 38.99.98.18
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/81458
Del: integrated -- https://master.php.net/note/delete/81458/integrated
Del: useless -- https://master.php.net/note/delete/81458/useless
Del: bad code -- https://master.php.net/note/delete/81458/bad+code
Del: spam -- https://master.php.net/note/delete/81458/spam
Del: non-english -- https://master.php.net/note/delete/81458/non-english
Del: in docs -- https://master.php.net/note/delete/81458/in+docs
Del: other reasons-- https://master.php.net/note/delete/81458
Reject -- https://master.php.net/note/reject/81458
Search -- https://master.php.net/manage/user-notes.php