Beispiel für einen Error Handler.
<?php
class MyErrorHandler
{
var $ErrorStack = array(
'Fatal Error' => array(),
'Warning' => array(),
'Notice' => array(),
);
var $Flag = true;
var $FlagSilent = true;
var $css = '
<style type="text/css">
div.errordiv{padding:10px;margin:2px;border:1px solid #999;}
div.errordiv strong{color:#c00;}
div.errordiv dd{font-weight:bold;}
</style>
';
function MyErrorHandler()
{
set_error_handler (array(&$this, 'handleError'));
register_shutdown_function(array(&$this, 'printErrors'));
}
function countError($arr)
{
$key = md5(serialize($arr));
if (isset($this->ErrorStack[$arr[0]][$key])){
$this->ErrorStack[$arr[0]][$key][4] ++;
}else{
$arr[4] = 1;
$this->ErrorStack[$arr[0]][$key] = $arr;
}
}
function handleError ($error_level, $error_message, $file, $line)
{
$EXIT = FALSE;
# Only handle the errors specified by the error_reporting directive or function
# Ensure that we should be displaying and/or logging errors
if ( ! ($error_level & error_reporting ()) || ! (ini_get ('display_errors') || ini_get ('log_errors')))
return;
# Give the error level a name
# Include the bitmask value for reference
# Set a switch indicating whether the error level should make the script exit
switch ($error_level) {
case E_NOTICE:
case E_USER_NOTICE:
$error_type = 'Notice';
break;
case E_WARNING:
case E_USER_WARNING:
$error_type = 'Warning';
break;
case E_ERROR:
case E_USER_ERROR:
$error_type = 'Fatal Error';
$EXIT = TRUE;
break;
# Handle the possibility of new error constants being added
default:
$error_type = 'Unknown';
$EXIT = TRUE;
break;
}
if (ini_get ('display_errors'))
if ($this->Flag){
$this->countError(array($error_type, $error_message, $file, $line));
}else{
$this->displayError($error_type, $error_message, $file, $line);
}
if (ini_get ('log_errors'))
error_log (sprintf ("%s: %s in %s on line %d", $error_type, $error_message, $file, $line));
if (TRUE == $EXIT)
exit;
}
function getErrorMessage($arr)
{
list($error_type, $error_message, $file, $line, $times) = $arr;
$base = basename($file);
$msg = <<< EOT
<div class="errordiv">
<dl>
<dt>Fehler</dt><dd><strong>$error_message</strong></dd>
<dt>Zeile</dt><dd>$line</dd>
<dt>Datei</dt><dd>$base</dd>
<dt>Pfad</dt><dd>$file</dd>
<dt>Anzahl</dt><dd>$times</dd>
</dl>
</div>
EOT;
return $msg;
}
function displayError($error_type, $error_message, $file, $line, $times)
{
$msg = $this->getErrorMessage(array($error_type, $error_message, $file, $line, $times));
print $msg;
}
function printErrors()
{
$report = '';
$report .= $this->css;
foreach ($this->ErrorStack as $type => $arr){
$report .= sprintf ('<h1>%s (%d)</h1>', $type, count($arr));
foreach ($arr as $err){
$report .= $this->getErrorMessage($err);
}
}
print $report;
}
} /* END CLASS */
$obj = new MyErrorHandler();
error_reporting (E_ALL);
# Trigger some errors to test the handler
trigger_error ('Trigger error was called with a simple error message');
# Use an undefined variable
print $var;
trigger_error ('Trigger a warning', E_USER_WARNING);
# Try to connect to a MySQL database server
mysql_connect ('localhost', 'invalid_user', 'invalid_pass');
$a = array("2");
$b = a . "l";
for ($i=0;$i<10;$i++){
$a[f] = $b[bb];
}
trigger_error ('Trigger a fatal error', E_USER_ERROR);
?>