Question

Question is, does it make sense, and how to, free memory and destroy / unset objects? Does exit() kill the app and nothing else has to be done?

Was it helpful?

Solution

In the manual, it states:

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit() is called.

So it is sufficient to just call exit();

However, if you are using PHP to power a web app, calling of exit isn't necessary because php will showdown after the request is made and the response is returned.

OTHER TIPS

It does make sense to use unset though in some cases when your dealing with a large quantity of objects that are getting left in memory while your script executes. If you do not cleanup after such methods you can hit the PHP memory limit set in php.ini which will terminate your script.

Things such as reading in files using file_get_contents can cause this too if you let the variable hang around that contains the file's contents.

It is rather rare though to see this occur, but best practice is to code for the future and clean up after yourself.

But back to the original question, no, PHP cleans everything up when the script terminates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top