PHP
  Home arrow PHP arrow Page 8 - Building A Generic Error Reporting Class In PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 20
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT