PHP
  Home arrow PHP arrow Page 8 - Building A Generic Error Reporting Cla...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building A Generic Error Reporting Class In PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2002-12-11

    Table of Contents:
  • Building A Generic Error Reporting Class In PHP
  • Back To Class
  • The Bare Bones
  • How Things Work
  • The Number Game
  • Running On Empty
  • Raising An Alarm
  • A Well-Formed Idea
  • Going To The Source

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building A Generic Error Reporting Class In PHP - A Well-Formed Idea


    (Page 8 of 9 )

    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


     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT