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:
<?php
class errorReporter
{
// non-fatal error handler
// clear previously-drawn
screen
// build an array of non-fatal errors
// and then die()
function displayNonFatalErrors()
{
ob_clean();
$errorList
= array();
for($x=0; $x<$this->numErrors(); $x++)
{
$errorType =
$this->_errorTypes[$this->_getErrorType($this->_errors[$x]["subtype"])];
$errorCode
= $this->_errors[$x]["subtype"];
$errorMsg =
$this->_errorsubTypes[$this->_errors[$x]["subtype"]];
$addInfo
= $this->_errors[$x]["data"];
$errorList[] = array($errorType, $errorCode,
$errorMsg,
$addInfo);
}
include("error-non-fatal.php");
die;
}
// snip
}
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:
<html>
<head>
</head>
<body>
<?
if (!$_POST['submit'])
{
?>
<form
method="POST" action="<?=$_SERVER['PHP_SELF']?>">
Name:
<br>
<input
type="text"
name="name">
<p>
Favourite sandwich filling:
<br>
<input
type="text"
name="filling">
<p>
Favourite ice-cream flavour:
<br>
<input
type="text"
name="flavour">
<p>
<input type="submit" name="submit"
</form>
<?
}
else
{
//
include class
include_once("errorReporter.class.php");
// initialize error reporter
$e
= new errorReporter();
// perform form validation on data entered by user
if
(empty($_POST['name']))
{
$e->raiseError(1101, "NAME");
}
if (empty($_POST['filling']))
{
$e->raiseError(1101,
"FILLING");
}
if (empty($_POST['flavour']))
{
$e->raiseError(1101, "FLAVOUR");
}
//
check to see if any errors
// display if so
if ($e->numErrors() > 0)
{
$e->displayNonFatalErrors();
}
//
if we get this far, it means
// there were no errors in the form data
// now
let's insert this data into a database
$name = $_POST['name'];
$filling = $_POST['filling'];
$flavour
= $_POST['flavour'];
$connection = mysql_connect("localhost", "user", "pass")
or
$e->raiseError(102, "LOCALHOST");
mysql_select_db("users") or $e->raiseError(104,
"USERS"); $query =
"INSERT INTO profile (name, filling, flavour) VALUES ('$name',
'$filling',
'$flavour')"; $result = mysql_query($query) or
$e->raiseError(101, $query . mysql_error());
//
all done?
// display success code
echo "Success!";
}
?>
</body>
</html>
And here's an example of the error screen you might see: