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?
use register_shutdown_function()
:
http://docs.php.net/manual/en/function.register-shutdown-function.php
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With