I’ve been doing some research on php error handling as well as exception handling.
For instance, to handle user errors, it is best to use set_error_handler() for user errors. Example code:
// Destinations
define("ADMIN_EMAIL", "nobody@stanford.edu");
define("LOG_FILE", "/my/home/errors.log");
// Destination types
define("DEST_EMAIL", "1");
define("DEST_LOGFILE", "3");
/**
* my_error_handler($errno, $errstr, $errfile, $errline)
*
* Author(s): thanosb, ddonahue
* Date: May 11, 2008
*
* custom error handler
*
* Parameters:
* $errno: Error level
* $errstr: Error message
* $errfile: File in which the error was raised
* $errline: Line at which the error occurred
*/
function my_error_handler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
// Send an e-mail to the administrator
error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);
// Write the error to our log file
error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
break;
case E_USER_WARNING:
// Write the error to our log file
error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
case E_USER_NOTICE:
// Write the error to our log file
error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
default:
// Write the error to our log file
error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
}
// Don't execute PHP's internal error handler
return TRUE;
}
// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler("my_error_handler");
Code found at http://www.stanford.edu/dept/its/communications/webservices/wiki/index.php/How_to_perform_error_handling_in_PHP.
Then for fatal errors:
register_shutdown_function('handleShutdown');
function handleShutdown() {
$error = error_get_last();
if($error !== NULL){
$info = "[SHUTDOWN] file:".$error['file']." | ln:".$error['line']." | msg:".$error['message'] .PHP_EOL;
yourPrintOrMailFunction($info);
}
else{
yourPrintOrMailFunction("SHUTDOWN");
}
}
Code found at How do I catch a PHP Fatal Error.
From what I can tell, it appears that this will cover essentially all errors that can occur within a script (as a very general statement – it’s hard to predict all errors). When developing a class, thought, it seems that it is suggested to use exceptions to handle them internally.
My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.
In any case, are these functions attached to all files or set within a class?
Any help is appreciated.
Edit: I also meant to add, how would I stop a script if a certain error is encountered. Obviously, exit() or die() does the trick, but is there anything more appropriate to do so?
Yes it is. You just have to be sure that you catch ALL generated exception.
I also want to remember you that if there is a synthax error in your scrip, or if your server is bad configured, php could output errors that are not handled because they are compile time errors.
Once you registered them (with set_error_handler) they are applied to every files that are called in your script apart from if one of your file redifine the handler.
and to stop a script, exit is the good way