Search code examples
phpgarbage-collection

Can I trust PHP __destruct() method to be called?


In PHP5, is the __destruct() method guaranteed to be called for each object instance? Can exceptions in the program prevent this from happening?


Solution

  • The destructor will be called when the all references are freed, or when the script terminates. I assume this means when the script terminates properly. I would say that critical exceptions would not guarantee the destructor to be called.

    The PHP documentation is a little bit thin, but it does say that Exceptions in the destructor will cause issues.