Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Anywho, 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!

like image 754
Logan Bibby Avatar asked Aug 08 '10 12:08

Logan Bibby


People also ask

What happens when destructor is not called?

A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() . If you do not define a destructor, the compiler will provide a default one; for many classes this is sufficient.

Is the destructor always called?

No. You never need to explicitly call a destructor (except with placement new ). A derived class's destructor (whether or not you explicitly define one) automagically invokes the destructors for base class subobjects. Base classes are destructed after member objects.

How many times destructor is invoked?

Why is the destructor being called three times?

Does PHP support destructor?

PHP - The __destruct Function A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script.


2 Answers

  • 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.

like image 145
Pekka Avatar answered Oct 12 '22 23:10

Pekka


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

like image 33
Cow Moo Avatar answered Oct 13 '22 01:10

Cow Moo