Code: Select all
class Error {
// Class properties
var $strReturnPage;
// Class: Error
// Method Desscription: class constructor
function Error() {
// set the handler
set_error_handler(array($this, 'CustomHandler'));
}
// Class: Error
// Method Desscription: custom error handler overwrite the default php error handler
function CustomHandler($errno, $errmsg, $filename, $linenum, $vars) {
//time stamp for the error
$dteToday = date("Y-m-d H:i:s (T)");
echo $this->strReturnPage;
// define an assoc array of error string
$errortype = array (
E_ERROR => "Error",
E_WARNING => "Warning",
E_PARSE => "Parsing Error",
E_NOTICE => "Notice",
E_CORE_ERROR => "Core Error",
E_CORE_WARNING => "Core Warning",
E_COMPILE_ERROR => "Compile Error",
E_COMPILE_WARNING => "Compile Warning",
E_USER_ERROR => "User Error",
E_USER_WARNING => "User Warning",
E_USER_NOTICE => "User Notice"
);
// set of errors for which a var trace will be saved
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
//depending one the errno
if ($errno == E_USER_ERROR) {
//this is a critical user error, so i will email it;
//create the error string
$strError = "Date: ".$dteToday."\r\n";
$strError .= "Error num: ".$errno."\r\n";
$strError .= "Error type: ".$errortype[$errno]."\r\n";
$strError .= "Error message: ".$errmsg."\r\n";
$strError .= "File: ".$filename."\r\n";
$strError .= "Line: ".$linenum."\r\n";
//email error
$_blnEmailSent = @mail(SITE_MAIL_ERROR,'Error Alert',$strError);
//create redirect to the default error page
$_SESSION[ERROR] = $errmsg;
$_strRedirectLink = "Location: ".SITE_PATH."/error.php";
$this->strReturnPage = null;
header($_strRedirectLink);
die();
}
else {
//i have a warning so i will just load the session with the error
$_SESSION[ERROR] = $errmsg;
//I have a return page so i must execute the redirect
$_strRedirectLink = "Location: ".SITE_PATH.$this->strReturnPage.".php";
$this->strReturnPage = null;
header($_strRedirectLink);
die();
}
}
}After some research i had to modify my code to look like:
Code: Select all
class Error {
// Class properties
var $strReturnPage;
// Class: Error
// Method Desscription: class constructor
function Error() {
// set the handler
}
function SetHandler() {
set_error_handler(array($this, 'CustomHandler'));
}Code: Select all
$objError->strReturnPage = "page.php";
$objError->SetHandler();
trigger_error("An error has been triggered",E_USER_ERROR);