HomePHP Page 4 - Building A Generic Error Reporting Class In PHP
How Things Work - 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.
Now, before proceeding further, I need to decide how this class is going to work. Here's how I plan to use it:
<?php
include_once("errorReporter.class.php");
$e = new errorReporter();
//
errors in this section would be considered fatal
// open connection
$connection
= mysql_connect("localhost", "user", "pass") or
$e->raiseError(102, "LOCALHOST");
mysql_select_db("store")
or $e->raiseError(104, "STORE");
// formulate and execute query
$query = "SELECT
id, label, desc, price,img_small FROM catalog LIMIT
0,3"; $result = mysql_query($query)
or $e->raiseError(101, $query .
mysql_error());
// iterate through result set
//
and print data for each item
// data consists of image, description, item ID
and
price
while(list($id, $label, $desc, $price, $img) = mysql_fetch_row($result))
{
?> <tr>
<td>
<img src="<? echo $img; ?>">
</td>
<td>
<a
href="details.php?id=<? echo $id; ?>"><? echo $label; ?></a>
<br>
<?
echo $desc; ?>
<br>
<i>Only <? echo $price; ?></i>
</td>
</tr>
<?
}
//
clean up
mysql_close($connection);
// check for missing form variables
// errors
in this section would be considered non-fatal
if (!$sku) { $e->raiseError(1101,
"SKU"); }
if (!$price) { $e->raiseError(1101, "PRICE"); }
// display non-fatal
errors (if any)
if ($e->numErrors() > 0)
{
$e->displayNonFatalErrors();
}
//
if we get this far, it means the script encountered
// zero fatals and zero non-fatals
//
in other words, the script was successfully executed
// display success page
include("success.tmpl");
?>
As you can see, I would like to wrap each function call in my script with a call
to the errorReporter class. If a function call fails, an error will be generated via a unique error code and added to the error stack. If the error is evaluated as fatal, an appropriate error message will be immediately displayed to the user; if the error is evaluated as non-fatal, script processing will continue and the developer has the option to throw up an error screen at a later date containing a list of non-fatal errors.
Once the basic functionality of the class is clear, it's a good idea to spend some time listing the important methods, together with their purpose. Here's my initial cut:
raiseError($code, $data) - raise an error using a pre-defined error code and optional debug data;
numErrors() - check the number of errors currently held in the error stack;
displayFatalError($code, $data) - display the fatal error screen;
displayNonFatalErrors() - display the non-fatal error screen;
flushErrors() - reset the error stack.
These are the essential methods; there may be more, which I will add as development progresses.