Created
August 9, 2013 04:56
-
-
Save christianchristensen/6191284 to your computer and use it in GitHub Desktop.
Error handling with PHP example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Setup | |
set_time_limit(3); | |
// http://php.net/manual/en/function.register-shutdown-function.php | |
function shutdown() { | |
global $start; | |
$elapsed=microtime(true) - $start; | |
echo "SHUTDOWN. Time: ${elapsed}"; | |
var_dump(error_get_last()); | |
} | |
register_shutdown_function("shutdown"); | |
// http://php.net/manual/en/function.set-error-handler.php | |
function error_handler($errno, $errstr, $errfile, $errline, $errcontext) { | |
global $start; | |
$elapsed=microtime(true) - $start; | |
echo "ERROR HANDLER. Time: ${elapsed}"; | |
var_dump(get_defined_vars()); | |
} | |
set_error_handler('error_handler', -1); | |
// Start "app" | |
global $start; | |
$start = microtime(true); | |
echo "Hi\n"; | |
$begin=time(); | |
// errors | |
echo $unknown; # force an error message | |
sleep(10); # CPU time suspended as wall time ticks (same as DB query?) | |
while (true) {} # force a PHP timeout | |
echo "Foo"; | |
## Output | |
# | |
# Hi | |
# ERROR HANDLER. Time: 3.1... | |
# ["errstr"]=> | |
# string(27) "Undefined variable: unknown" | |
# ... | |
# SHUTDOWN. Time: 13.4... | |
# ["message"]=> | |
# string(44) "Maximum execution time of 3 seconds exceeded" | |
# ... | |
############# | |
## References | |
# http://stackoverflow.com/questions/6217994/php-how-to-use-set-error-handler-to-properly-deal-with-all-errors-except-noti | |
# http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function | |
# http://stackoverflow.com/questions/36621/set-error-handler-isnt-working-how-i-want-it-to-work | |
# http://stackoverflow.com/questions/236795/php-destructor-vs-register-shutdown-function | |
# https://github.com/panrafal/PHP-Error | |
# http://stackoverflow.com/questions/1900208/php-custom-error-handler-handling-parse-fatal-errors | |
# http://stackoverflow.com/questions/4657475/register-shutdown-function-what-is-best-practice | |
# http://www.mysqlperformanceblog.com/2008/05/20/apache-php-mysql-and-runaway-scripts/ | |
# https://gist.github.com/jsjohnst/3297804 | |
# http://stackoverflow.com/questions/1870125/how-to-avoid-blocking-indefinitely-when-using-a-php-script-to-update-a-db-table | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment