Reputation: 12566
Is there a way to tell PHP to run one last function, or somehow override some error handler in the event of a fatal error? I have a cron that has a main entry point of:
$obj = new obj();
$obj->run();
exit;
Would wrapping that in a try catch do the trick if I'm not explicitly throwing errors? All I want is for PHP to do something as simple as create a txt file containing the fatal error, or even just create an empty file named 'failed.txt', or something.
Thanks!
Upvotes: 3
Views: 204
Reputation: 2100
You can do custom error handling in PHP. Check the manual at: http://php.net/function.set-error-handler. This is a fairly straightforward application of that.
Upvotes: 1
Reputation: 3278
error_handler might help you here
Or this for fatal errors: http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
Upvotes: 3
Reputation: 24969
Use register_shutdown_function()
to register a function to execute as the last call. This will get call whether there was an error or not.
See http://php.net/manual/function.register-shutdown-function.php for more information.
Upvotes: 1
Reputation: 1936
You can set a custom error handler for most errors, and execute some code there.
But cannot handle an E_ERROR
which is what your fatal error is likely to be.
Imagine if that error was for running out of memory. You try to handle that error, and end up using more memory. Which just throws another error, and you're right where you started.
Upvotes: 2
Reputation: 197692
You write it's cron
. Call the script with a path to a new php.ini
file wherein you make PHP to log the errors to STDERR
or STDOUT
.
php -h
will give you all commandline options.
Cron
will send you the reports then if something failed by email.
Alternatively you can set this up to log into your own log as it's a new php.ini
. In case that's easier for you to review.
Upvotes: 3