Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Building A Generic Error Reporting Class In PHP
  2. Back To Class
  3. The Bare Bones
  4. How Things Work
  5. The Number Game
  6. Running On Empty
  7. Raising An Alarm
  8. A Well-Formed Idea
  9. Going To The Source
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 21
December 11, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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:

<html> <head> <basefont face="Arial"> </head> <body> <p> <p> <table width="100%" border="0" cellspacing="0" cellpadding="5" bgcolor="Navy"> <tr> <td bgcolor="Navy" width="100%"><font color="white"><b>Error!</b></font></td> </tr> </table> <p> The following non-fatal errors occurred: <ul> <? foreach ($errorList as $e) { ?> <li><b><?=$e[0]?> (<?=$e[1]?>)</b> <br> <?=$e[2]?> (<?=$e[3]?>) <? } ?> </ul> </body> </html>
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:



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: