PHP
  Home arrow PHP arrow Page 4 - 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 - 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

    - 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 5 hosted by Hostway
    Stay green...Green IT