I made my own error handling class, and tested it on UniServer using PHP version 5. It worked fine, but then I tried it on a server running PHP 4 and it didn't work.
I did some reading up on the subject and read that once you take over from PHP’s default error handling function, you cannot change class vars. So how on earth can you make a error handling class, which will wait till the end of the page, then display all errors, using trigger_error?
Below is my class
class errorHandling
{
// Define array varibles to store errors
var $error_array;
// Debug level for class
var $dubug_level = 1;
// Define config variables
// Output to a log file
var $log = FALSE;
// Log file path
var $log_path = "/var/tmp/site-errors.log";
// Email to send errors to, to enable set $email to an email address
var $email = FALSE;
/**
* A Constructor function
*/
function errorHandling()
{
// Take over from PHP error handling
set_error_handler(array($this, 'handle_error'));
}
/**
* Works out what errors should be reported
* given a certain dubug code value
*/
function report_level()
{
// Array of all possible error codes
$error_codes = array(1024,512,256,128,64,32,16,8,4,2,1);
$tmp_value = $this->debug_level;
// If debug level = 2047 return all errors codes
if($tmp_value=='2047')
{
return $error_codes;
}
else
{
// Loop through all error codes
foreach($error_codes as $key => $value)
{
// If when you add the values, it is less than or equal to 2047 it means
// this code is not wanted, => remove this key from array
$tmp_value = $tmp_value + $value;
if($tmp_value<=2047)
{
unset($error_codes[$key]);
}
else
{
// Return tmp_value to old value before comparison
$tmp_value = $tmp_value - $value;
}
}
// Return array of all error codes which should be reported
return $error_codes;
}
}
/*
* Function for php to use to add a new error msg
*/
function handle_error($type, $string, $file, $line, $vars)
{
// Different error codes and a english name
$errortype = array (
E_ERROR => "Error", // 1
E_WARNING => "Warning", // 2
E_PARSE => "Parsing Error", // 4
E_NOTICE => "Notice", // 8
E_CORE_ERROR => "Core Error", // 16
E_CORE_WARNING => "Core Warning", // 32
E_COMPILE_ERROR => "Compile Error", // 64
E_COMPILE_WARNING => "Compile Warning", // 128
E_USER_ERROR => "User Error", // 256
E_USER_WARNING => "User Warning", // 512
E_USER_NOTICE => "User Notice", // 1028
E_STRICT => "Runtime Notice" // 2048
);
// Add error to array
$this->error_array[] = array("Type" => $errortype[$type], "String" => $string, "File" => $file, "Line" => $line, "Vars" => $vars, "Code" => $type);
// Create Error Message
$error_msg = "\r\n[" . date("Y/m/d H:i:s",time()) . "] - " . $errortype[$type] . ": '". $string ."' in " . $file . " on line " . $line;
// If $this->log is set to TRUE
if($this->log)
{
// See if a log file exists
if(is_file($this->log_path))
{
error_log($error_msg,3,$this->log_path);
}
else
{
// Try to make log file
$fh=fopen($this->log_path,'w');
fclose($fh);
// Try to write to it again
if(is_file($this->log_path))
{
error_log($error_msg,3,$this->log_path);
}
}
}
}
/**
* Function to return error codes
*/
function get_errors()
{
// Get error codes which user wants to report to browser
$error_codes = $this->report_level();
// For each error check if its error code is one which is wanted to
// be reported, if so add to temp array
foreach($this->error_array as $key => $value)
{
if(in_array($value['Code'],$error_codes))
{
$temp_array[] = $value;
}
}
// If there are more than 0 errors return temp_array
if(count($temp_array)!=0)
{
// Email errors to admin email
if($this->email)
{
// Contruct email
$subject= SITE_TITLE . " : SITE ERROR REPORT";
$head="From: " . $_SERVER["SERVER_ADMIN"];
$emess .="Hi,";
$emess .="\n\nDuring a vist to your site errors occured.";
$emess .="\nA list of all errors is given below";
$emess .="\n\nErrors generated at " . get_time('datetime');
// Loop through error array
foreach($temp_array as $key => $value)
{
$emess .="\n" . $errortype[$type] . ": '". $string ."' in " . $file . " on line " . $line;
}
$emess .="\n\nEnd of error report.\n" . SITE_TITLE;
// Email webmaster
@mail($this->email,$subject,$emess,$head,"-f" . $_SERVER["SERVER_ADMIN"]);
}
return $temp_array;
}
else
{
return 0;
}
}
}