Are there any instances when the destructor in PHP is NOT called?
Asked Answered
S

3

16

This is my first time posting to stackoverflow, but these threads have helped me tremendously!

Anyhow, onto my question... Are there any instances when the destructor in PHP is NOT called? The reason I ask is because I have a mapper class which maps data to objects and in the constructor, I start a transaction and in the destructor I'll call a commit (I'll also have a member function which can also do the committal, if necessary). If there are any instances when the destructor isn't called, I'd like to know so I can anticipate it happening and plan appropriately.

Thanks very much!

Suttle answered 8/8, 2010 at 12:22 Comment(0)
C
17
  • According to the manual, destructors are executed even if the script gets terminated using die() or exit():

    The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.

  • According to this SO question, the destructor does not get executed when PHP's execution time limit is reached (Confirmed on Apache 2, PHP 5.2 on Windows 7).

  • The destructor also does not get executed when the script terminates because the memory limit was reached. (Just tested)

  • The destructor does get executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different

  • It does not get executed on parse errors (because the whole script won't be interpreted)

  • The destructor will certainly not be executed if the server process crashes or some other exception out of PHP's control occurs.

All in all, it looks pretty reliable.

The downside of doing things other than cleanup in the destructor, though, is that your options there are somewhat limited. You can't throw exceptions any more (except if you catch them again inside the destructor), you can't output any error messages, you can't really rely on the presence of other objects (like the database interface) any more ..... I don't have deep experience in working with destructors but I'm not sure whether what you're planning to do is a feasible idea.

Clyde answered 8/8, 2010 at 12:24 Comment(5)
Thank you! :) I had seen that snippet from the manual but figured I'd try and be safe by asking here as well. I just tested to see what happened when a fatal error occurred. All I did was created a member which attempts to call a non-existent member. The destructor was NOT called. It does, of course, get called with warnings and below. Otherwise, I can't think of any other instances that could be checked to see if it gets called. I'll just assume it does and do extensive testing! Thanks again!Suttle
@Logan you're welcome. Interesting thing about the fatal error: It worked for me when provoking one using $$fake(); Maybe it depends on the kind of error. Also, check out the paragraph I just added with some thoughts on the general idea.Clyde
thanks for the extra info! I just tested to see if what I could do in a destructor. So far, I know I can call a member function of the same class and a member function of a different class (instantiated from the constructor). Maybe I can do what I wanted to do, I'll post another comment once I find out for sure.Suttle
Different fatal errors are handle differently in PHP. Some (e.g. failed requires) even skip shutdown functions. It's best not to rely on any deterministic behavior in case of fatal error. Also, according to this bug report, the behavior might change whith xdebug enabled (though that is fixed in recent xdebug versions).Culminant
If you need the destructor to be called even in case of php fatal error then register it as shutdown handler in constructor: public function __construct() { register_shutdown_function(array($this, '__destruct')); }. The cost of this solution is that the object reference (and the object itself) exists till the end of php script execution. Still, there are case then it is worth of it - e.g. huge tmp files removal in destructor.Louannlouanna
I
2

I would just like to add, if you have a fatal error inside a destructor, it can stop other destructors from executing.

Involutional answered 26/5, 2011 at 20:35 Comment(0)
H
0

The destructor function (and also the constructor) is not called when a method is invoked statically within a class; For example:

class TestClass {

    public function __construct(){
        echo "constructor function called!<br>";
    }

    public static function test(){
        echo "static function called!<br>";
    }

    public function __destruct(){        
        echo "destructor function called!<br>";
    }
    
}

TestClass::test(); // does not call neither the constructor nor the destructor

But if you instantiate the class, both the constructor and the destructor are going to be called as expected.

$var = new TestClass;
$var->test();

But also, as noted by the accepted answer, they are not called upon time limit reached, fatal or parse errors and/or other expeptions/crashes.

Herpetology answered 27/3, 2023 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.