When is __destruct called in PHP?

PHP: When is __destruct called in PHP?

The __destruct method is called either during the shutdown sequence (i.e. end of execution of a script), or when there are no references to an object.

Remember that if you put a __destruct() method in a subclass, you must call parent::__destruct() if you want the parent's destruct method to execute (assuming there is one).

The method will be called even if exit() is called. However, if exit() is called from within a destructor, then no further destructors will be called.

Note: completely unrelated to Array Destructuring.

webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about When is __destruct called in PHP?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

What are the SPL Iterators in PHP?

What does 'final class' and 'final function' (final method) mean in PHP?

How do you run a PHP script from the command line?

How to check if a relation was loaded on an Eloquent model already?

How to check if a string contains a certain substring?

Force trailing slash (redirect if no trailing slash exists) in .htaccess

How to Extract Query String (from a URL) Into an Associative Array in PHP

Why should you update APP_URL from from http://localhost?

When is __destruct called in PHP?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?