hey all, i just wrote my first object and im curious if anyone willl look it over and check for efficiency. it's for error handling with loging, i just took the error codes and categories from a tutorial. feel free to use it. any input will be appreciated
example:
$r = new ErrorHandling();
$r->returnErrorText(101, 0, LINE, FILE);
output:
ERROR > 100 (MySQL Error) > 101: Error in SQL query > C:\www\root\hd\cls\test.php on line 2
code:
<?
/*
####################################################################################################
#
# Primary Coding: Richard Anderson
# Secondary Coding: None
#
# Date started: 9/15/03
# Date finished: 9/15/03
#
# Last mod: 9/16/03
# Last mod by: Richard Anderson
# Last mod note: Added method logErrors(). Logs errors to specified
# log file in HTML format.
#
####################################################################################################
*/
class ErrorHandling
{
// Array of error categories.
var $errorCat = array(
100 => "MySQL Error",
200 => "File I/O Error",
300 => "Authentication Error",
400 => "PHP Script Error",
1100 => "Input Form Error",
1200 => "URL Parse Error"
);
// Array of error descriptions.
var $errorArr = array(
101 => "Error in SQL query",
102 => "Could not connect to database.",
103 => "Database already exists.",
104 => "Could not select database.",
201 => "Could not create directory.",
202 => "Could not copy file(s).",
203 => "Could not execute command.",
204 => "Could not read configuration file.",
205 => "Could not delete file(s).",
206 => "Could not read file(s).",
207 => "Could not write file(s).",
208 => "Could not open file(s).",
209 => "File already exists.",
210 => "Could not delete directory.",
301 => "Session has expired.",
302 => "Invalid Username/Password.",
401 => "An internal script error has occured.",
1101 => "Required field was left empty.",
1102 => "Invalid data field.",
1103 => "File upload unsuccessful.",
1201 => "Missing record identifier."
);
// Reference for $errorInfo array
//
// 0 => error number
// 1 => fatal flag
// 2 => error description
// 3 => error category description
// 4 => line of error
// 5 => file of error
//
var $errorInfo;
var $errorNo;
var $errorDesc;
var $errorFatal;
var $errorCatNum;
// Constructor
//
function ErrorHandling()
{
$this->clearErrors();
}
// Build arrays for error handling
//
function buildErrorArray($errorNo, $errorFatal, $errorLine, $errorFile)
{
$this->clearErrors();
$this->errorNo = $errorNo;
$this->errorInfo[0] = $errorNo;
$this->errorInfo[1] = $errorFatal;
$this->errorInfo[2] = $this->errorArr[$this->errorNo];
$this->errorCatNum = substr($errorNo, 0, 1) . "00";
$this->errorInfo[3] = $this->errorCat[$this->errorCatNum];
$this->errorInfo[4] = $errorLine;
$this->errorInfo[5] = $errorFile;
}
// Spits out the error message as text.
//
function returnErrorText($errorNo, $errorFatal, $errorLine, $errorFile)
{
$this->clearErrors();
$this->buildErrorArray($errorNo, $errorFatal, $errorLine, $errorFile);
echo "<font size=2 color=000000 face=verdana><b>ERROR</b> > " . $this->errorCatNum . " (" . $this->errorInfo[3] . ") > " . $this->errorInfo[0] . ": " . $this->errorInfo[2] . " > " . $this->errorInfo[5] . " on line " . $this->errorInfo[4] . "</font><br>";
$this->logErrors();
if($errorFatal == 1)
{
exit();
}
}
// Spits out the error message as a javascript popup.
//
function returnErrorPopup($errorNo, $errorFatal, $errorLine, $errorFile)
{
$this->clearErrors();
$this->buildErrorArray($errorNo, $errorFatal, $errorLine, $errorFile);
echo "<script language=javascript>alert(\"" . addslashes(" ERROR > " . $this->errorCatNum . " (" . $this->errorInfo[3] . ") > " . $this->errorInfo[0] . ": " . $this->errorInfo[2] . " > " . $this->errorInfo[5] . " on line " . $this->errorInfo[4]) . "\");</script>";
$this->logErrors();
if($errorFatal == 1)
{
exit();
}
}
// Logs errors into file.
//
function logErrors()
{
$file = "./errors.html";
// if file doesn't exist create header for the file.
if(!file_exists($file))
{
$logThis .= "<html>\n <head>\n <title>title here</title>\n </head>\n<body bgcolor=ffffff>\n\n<font face=verdana size=2 color=000000>\n#################################################################################################";
$logThis .= "<br>\n# <br>\n# ErrorHandling.cls log file<br>\n# Written by: Richard Anderson<br>\n#<br>\n# This file will log all errors that were raised using this class from files in this directory.<br>\n# Log file started " . date("n/j/y g:ia");
$logThis .= "<br>\n#<br>\n#################################################################################################<br>\n";
}
$fp = fopen($file, "a+");
$logThis .= "<br>\n" . date("n/j/y @ g:i:sa") . " (" . time() . ") :: " . $_SERVER['REMOTE_ADDR'] . " :: " . $this->errorCatNum . " (" . $this->errorInfo[3] . ") :: " . $this->errorInfo[0] . " (" . $this->errorInfo[2] . ") :: " . $this->errorInfo[5] . " on line " . $this->errorInfo[4];
fwrite($fp, $logThis);
// Cleanup
unset($file);
unset($fp);
unset($logThis);
}
// Clears the errorInfo array;
//
function clearErrors()
{
unset($this->errorInfo);
$this->errorInfo = array();
}
// Clean UP
//
function destruct()
{
unset($this->errorInfo);
unset($this->errorArr);
unset($this->errorCat);
}
}
?>