Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function when script is terminated

Tags:

php

I'd like to call a function when the script is terminated by die() or exit().
My wish is to do this automatically, so I don't have to call function all the time.

Is this possible?

like image 808
usoban Avatar asked Aug 08 '09 11:08

usoban


2 Answers

use register_shutdown_function():

http://docs.php.net/manual/en/function.register-shutdown-function.php

like image 93
Scharrels Avatar answered Oct 18 '22 11:10

Scharrels


The __destruct() magic method for classes also runs at shutdown, unless you unset the class object first. Though Scharrels response is simpler if you just want to register a normal function. See http://docs.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor

One thing to bear in mind when running scripts in shutdown is this happens after the output is sent to the browser, so you won't get any errors displayed to screen. Make sure you remember to log errors to file for shutdown functions!

like image 22
simonrjones Avatar answered Oct 18 '22 11:10

simonrjones