im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire implicitly. Ive done all the reading on php.net and related sites, but i cant find an answer to this question.
If i have a simple class, something like:
class test{
public function __construct(){
print "contructing<br>";
}
public function __destruct(){
print "destroying<br>";
}
}
and i call it with something like:
$t = new test;
it prints the constructor message. However, i'd expect that when the scripts ends and the page is rendered that the destructor should fire. Of course it doesnt.
If i call unset($t); when the scripts ends, of course the destructor fires, but is there a way to get it to fire implicitly?
thanks for any tips
This is pretty easy to test.
In PHP 5.5.12 this prints:
So we can see that the destructor is called when we explicitly unset the object, when it goes out of scope, and when the script ends.
The __destruct method of a class is called once all references to the object are unset.
For example
The destructor is automatically called if the dummy object is set to null or the script is exited.
However, there are three crucial memory notes to calling the destructor method:
Firstly, the desctructor method should be a public method, not protected or private.
Secondly, refrain from using internal and circular references. For example:
The following will not work either:
Thirdly, deciding whether destructors are called after output is sent. Using
one can determine whether all destructors are called before sending data to user.
See http://php.net/manual/en/language.oop5.decon.php for the sources and elaborate explanations of magic destruct methods with examples.
My understanding is that destructors are automatically called for any remaining objects when the script ends.
Looking though the manual page on constructors and destructors, it seems the only way to bypass destructors entirely is if you call
exit()
from the destructor of an object that is destroyed prior to the object in question.Are you using
exit()
in any of your destructors? Are there even multiple objects in your script?If it's not too much trouble, perhaps you could post the actual code in question rather than the sample code you have in your question now. Aside from the typo in your sample constructor, that code should call both the constuctor and destructor for your
test
object.The
__destruct()
magic function is executed when the object is deleted/destroyed (usingunset
). It is not called during shutdown of a script. When a PHP script finishes executing, it cleans up the memory, but it doesn't 'delete' objects as such, thus the__destruct()
methods aren't called.You may be thinking of the
register_shutdown_function()
, which is fired when your PHP script finishes executing.