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

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
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.

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

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: