PHP
  Home arrow PHP arrow Page 4 - 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 - How Things Work
    ( Page 4 of 9 )

    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
     

       

    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 1 Hosted by Hostway
    Stay green...Green IT