8

Are there situations in which this method would not be called?

I'm thinking to store a important variable into a persistent cache just before the cache object gets destroyed. This variable is used many times in the page so I wouldn't like to update the cache with it every time the variable changes...

Alex
  • 66,732
  • 177
  • 439
  • 641
  • 1
    This might help: http://stackoverflow.com/questions/151660/php-destruct-method – stewe Feb 05 '12 at 20:32
  • Be aware that objects are destroyed in arbitrary order when the script terminates, so if storing something to a persistent cache requires access to a pre-initialized 'cache handler object' this could be a problem... – jgivoni Mar 28 '12 at 13:48
  • possible duplicate of [When will \_\_destruct not be called in PHP?](http://stackoverflow.com/questions/2385047/when-will-destruct-not-be-called-in-php) – Madara's Ghost Jun 03 '13 at 08:19

3 Answers3

14

Let's have a class:

class A {
    public function __construct(){
        echo "Construct\n";
    }

    public function __destruct(){
        echo "Destruct\n";
    }
}

And test code:

$test = new A();
die( "Dead\n");  // Will output Construct; dead; Destruct

$test = new A();
throw new Exception("Blah\n"); // Construct, Fatal error (no destruct)

$test = new A();
require_once( 'invalid_file.php'); // Construct, Fatal error (no destruct)

So basically: there are situations (fatal errors) when destructor won't be called.

Ah and this question has the same answer as this one: When will __destruct not be called in PHP? (+/-)

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • 3
    Personally, I'd consider that more of a programming error than a PHP unreliability. It's poor design to fail to catch a thrown Exception. – nickb Feb 05 '12 at 20:35
  • @nickb yes, you're right it's design and programming fail, but cases like `include 'fatal_error.php'` are real (especially when you're building modular application) and that would be my next test and the answer would remain the same. – Vyktor Feb 05 '12 at 20:40
  • I want to use __destruct() to free up locks at the end of a function. So I create a local variable `$lock = new TmpLock()` inside the function, and I expect `$lock->__destruct()` to be reliably called at the end of the function, not some time later. In 3v4l.org this works, but I don't know how reliable it is in general. Also my IDE will say I have an unused variable `$lock` which is only written and never read. If this does not work I need try/finally. – donquixote Apr 19 '23 at 17:06
3

It is called as soon as there are no more references to that particular object, or during the shutdown sequence. The manual also states destructors are called when scripts are terminated with exit().

Aside from the issue pointed out by TimWolla, I am not aware of any problems with PHP destructors.

drew010
  • 68,777
  • 11
  • 134
  • 162
1

It seems there at least was a problem using Windows: https://github.com/WoltLab/WCF/blob/ff7e6ed381f2ccab7f51220f97087921133b2237/wcfsetup/install/files/lib/system/WCF.class.php#L122

I don't know whether this is still relevant.

TimWolla
  • 31,849
  • 8
  • 63
  • 96