HomePHP Page 8 - Building A Generic Error Reporting Class In PHP
A Well-Formed Idea - PHP
The traditional method of building dynamic, PHP-based Web sites - mixing HTML elements with PHP code - can result in mangled Web pages (and much user angst) if errors take place during script execution. But yes, you can avoid the ugliness - plug in our handy error reporting class, which provides a simple way of trapping script errors and generating consistent, user-friendly error screens.
Unlike fatal errors, which immediately cause script termination when they are raised, non-fatal errors merely get registered in the error stack - the developer is free to display these non-fatal errors at any time (or even ignore them altogether).
Here's what the displayNonFatalErrors() method looks like:
// non-fatal error handler
// clear previously-drawn
// build an array of non-fatal errors
// and then die()
for($x=0; $x<$this->numErrors(); $x++)
$errorList = array($errorType, $errorCode,
The only difference between the internals of this method, and those of the displayFatalErrors()
method discussed on the previous page, is that this method builds and returns an array of one or more non-fatal errors rather than a single error. The output buffer is cleared of previously-generated data, and then replaced by a non-fatal error template, which takes care of formatting and neatly displaying the list of errors. Script execution is then terminated.
Here's what my non-fatal error template looks like:
Pretty simple, this - this script accepts the $errorList array created by the
displayNonFatalErrors() method and iterates through it to create a bulleted list of errors.
It should be noted that I added this type of error primarily to handle form validation tasks - if you have a large or complex form to validate, it's inconvenient to terminate script processing every time a validation routine fails (as would happen if I only used fatal errors). Non-fatal errors make it possible to perform a series of validation routines on form data, store the errors that occur in an array, and then display all the errors at one time for the user to view and correct.
Here's an example of how you could use the errorReporter's non-fatal errors while performing form validation:
Favourite sandwich filling:
Favourite ice-cream flavour:
<input type="submit" name="submit"
// initialize error reporter
= new errorReporter();
// perform form validation on data entered by user
check to see if any errors
// display if so
if ($e->numErrors() > 0)
if we get this far, it means
// there were no errors in the form data
let's insert this data into a database
$name = $_POST['name'];
$filling = $_POST['filling'];
$connection = mysql_connect("localhost", "user", "pass")
mysql_select_db("users") or $e->raiseError(104,
"USERS"); $query =
"INSERT INTO profile (name, filling, flavour) VALUES ('$name',
'$flavour')"; $result = mysql_query($query) or
$e->raiseError(101, $query . mysql_error());
// display success code
And here's an example of the error screen you might see: