thepadders
asked on
Error Handling
In PHP5 if you return false out of the function referenced by set_error_handler the default PHP error handler takes over and handles everything. Is there a way to do this in PHP4? Or perhaps anyone has the default error handler code written in PHP?
ASKER
Yes, I understand all that but it dosen't really help answer my question unfortunatly.
I don't understand your question. My bad english, sorry. Some one will answer.
No if you use a error-handler in php4 the returnvalue of your function is not evaluated by php4 and thus you need to take care of it in your error-handler (e.g. print out the error-message)
The whole point of set_error_handler is to offer a way for you to handle errors why would you set a return value?
As an Example :
========================== ========== ========== ========== ========== ========== ========== ===
function errMsg ($err_type, $err_msg, $err_file, $err_line) {
$errortype = array (
1 => "Error",
2 => "Warning",
4 => "Parsing Error",
8 => "Notice",
16 => "Core Error",
32 => "Core Warning",
64 => "Compile Error",
128 => "Compile Warning",
256 => "User Error",
512 => "User Warning",
1024=> "User Notice",
2048=> "Strict Error"
);
if ($err_type <= error_reporting()) {
// We Need to Handle this Error
echo (" A $errortype[$err_type] has occured in <b>$err_file</b> at line <b>$err_line</b> <br />
$err_msg
");
// Log Error
$error_text = "ERROR DATE TIME : ".date("d/m/Y g:m A")."\n";
$error_text .= "ERROR TYPE WAS : $error_name\n";
$error_text .= "ERROR OCCURED AT : $err_file - $err_line\n";
$error_text .= "ERROR WAS : $err_msg\n\n";
error_log($error_text,3,"c :/temp/err orlog.txt" );
}
}
set_error_handler("errMsg" ); // Set Error Handler
========================== ========== ========== ========== ========== ========== ========== ===
As an Example :
==========================
function errMsg ($err_type, $err_msg, $err_file, $err_line) {
$errortype = array (
1 => "Error",
2 => "Warning",
4 => "Parsing Error",
8 => "Notice",
16 => "Core Error",
32 => "Core Warning",
64 => "Compile Error",
128 => "Compile Warning",
256 => "User Error",
512 => "User Warning",
1024=> "User Notice",
2048=> "Strict Error"
);
if ($err_type <= error_reporting()) {
// We Need to Handle this Error
echo (" A $errortype[$err_type] has occured in <b>$err_file</b> at line <b>$err_line</b> <br />
$err_msg
");
// Log Error
$error_text = "ERROR DATE TIME : ".date("d/m/Y g:m A")."\n";
$error_text .= "ERROR TYPE WAS : $error_name\n";
$error_text .= "ERROR OCCURED AT : $err_file - $err_line\n";
$error_text .= "ERROR WAS : $err_msg\n\n";
error_log($error_text,3,"c
}
}
set_error_handler("errMsg"
==========================
You set an return value, so the default php-error-handler can be used (e.g. you only want to catch errors from cetrain files by your own) As php has some more options to handle errors (e.g. write to webserver-log) this can be a very usefull option.
ASKER
Yes, hernst42 understands the reason; I guess there is no easy work around to do this, will just have to wrap the whole thing in a check for PHP5.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
5.0.0 The error_types parameter was introduced.
4.3.0 Instead of a function name, an array containing an object reference and a method name can also be supplied as the error_handler.
4.0.2 Three optional parameters for the error_handler user function was introduced. These are the filename, the line number, and the context.
http://en.php.net/set_error_handler
The above link contains also an example to handle errors with the set_error_handler.
Hope it helps.