I have a PHP class that creates a PNG image on the fly and sends it to browser. PHP manual says that I need to make sure that imagedestroy function is called at end to release the memory. Now, if I weren't using a class, I would have some code like this:
function shutdown_func()
{
global $img;
if ($img)
imagedestroy($img);
}
register_shutdown_function("shutdown_func");
However, I believe that appropriate place for my class would be to place a call to imagedestroy in class' destructor.
I failed to find out if destructors get called the same way shutdown functions does? For example, if execution stops when user presses the STOP button in browser.
Note: whatever you write in your answer, please point to some article or manual page (URL) that supports it.
I just tested with Apache, PHP being used as Apache module. I created an endless loop like this:
<?php
class X
{
function __destruct()
{
$fp = fopen("/var/www/htdocs/dtor.txt", "w+");
fputs($fp, "Destroyed\n");
fclose($fp);
}
};
$obj = new X();
while (true) {
// do nothing
}
?>
Here's what I found out:
However, doing this:
<?php
function shutdown_func() {
$fp = fopen("/var/www/htdocs/dtor.txt", "w+");
fputs($fp, "Destroyed2\n");
fclose($fp);
}
register_shutdown_function("shutdown_func");
while (true) {
// do nothing
}
?>
shutdown_func gets called. So this means that class destuctor is not that good as shutdown functions.
Based on the principle that you should finish what you start, I'd say the destructor is the correct place for the free call.
The destructor will be called when the object is disposed of, whereas a shutdown function will not be called until script execution finishes. As noted by Wolfie, these won't necessarily happen if you forcibly halt the server or the script, but at that time, the memory allocated by PHP will be freed anyway.
Also noted by Wolfie, PHP will free up script resources when the script closes, so if you're only instantiating one of these objects, then you probably wouldn't notice a massive difference. However, if you later do end up instantiating these things, or do so in a loop, then you probably don't want to have to worry about a sudden spike in memory usage, so for the sake of future sanity, I return to my original recommendation; put it in the destructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With