When will __destruct not be called in PHP? When will __destruct not be called in PHP? php php

When will __destruct not be called in PHP?


The __destruct will not be called:

  • If exit is called in another destructor
  • Depending on the PHP Version: if exit is called in a shutdown function registered with register_shutdown_function
  • If there is a fatal error somewhere in the code
  • If another destructor throws an exception
  • If you try to handle an exception in a destructor (PHP >= 5.3.0)

Guess that's all I can think of right now

& What Pascal MARTIN said. That's the first step of debugging that.


The __destruct method will also not be called if script is running on CLI and receives a SIGTERM (Ctrl+C)


Not having an output on the screen doesn't mean the destructor is not called : the ouptut could be captured using output_buffering (maybe lime does that, to be able to work on it ? ), and not echoed when the script ends, for instance.

For testing purposes, you could try writing to a file, in your __destruct method, instead of just echoing some text.
(Just make sure your application / PHP has the required privileges to write to your destination file)

(I've already run into situations where I would not see the output made in a destructor -- but it was actually called)