PHP
  Home arrow PHP arrow Page 4 - Error Handling in PHP: Coding Defensiv...
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

Error Handling in PHP: Coding Defensively
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 38
    2006-01-11

    Table of Contents:
  • Error Handling in PHP: Coding Defensively
  • Killing your scripts: basic error handling with the “die()” statement
  • Triggering errors: more efficient error handling with the “trigger_error()” function
  • The PEAR way: looking at the PEAR_Error object
  • True and false: handling errors with Boolean flags

  • 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


    Error Handling in PHP: Coding Defensively - The PEAR way: looking at the PEAR_Error object


    (Page 4 of 5 )

    In PHP 4 object-oriented scenarios, one of the most popular approaches for handling errors is the PEAR_Error object. While not all developers feel comfortable working with PEAR packages, admittedly it’s a very strong framework that can be used in numerous applications. Specifically, the PEAR_Error object is extremely useful for handling errors, and certainly provides a lot of information that you might need. Let’s have a look at its methods:

    - PEAR::getMessage(): returns the error message.
    - PEAR::getType(): returns the PEAR_Error subtype.
    - PEAR::getUserInfo(): returns additional information on the error and the context where it took place
    - PEAR::getCode(): return the error code. Eventually, this method might return no values.

    Having described the corresponding object methods, the next step will consist of modifying the sample class, so it can return a PEAR_Error object, instead of using the “trigger_error()” function:

    // include PEAR library
    require_once("PEAR.php");

    class FileReader{
      var $file;
      var $fileDir='fileDir/';
      function FileReader($file){
        if(!file_exists("{$this->fileDir}{$file}.php")){
          return PEAR::RaiseError('File '.$file. ' not found');
        }
        $this->file=$file;
      }
      function getContent(){
        if(!$content=file_get_contents("{$this->fileDir}{$this->file}.php")){
          return PEAR::RaiseError('Unable to read file contents');
        }
        return $content;
      }
    }

    As you can see, now the above class returns a PEAR_Error object when things go wrong. Given the existence of this object, I’m able to call one of its methods, as follows:


    $fr=new FileReader('inexistent_file');
    echo $fr->getContent();
    if (PEAR::isError($fr)) {
        echo $fr->getMessage()."\n";
        exit();
    }

    The above example demonstrates how to check for the existence of an error object, and accordingly call its “getMessage()” method. Evidently, this method provides much more flexibility and allows you to obtain specific information on both the nature of the error and the context in which it took place.

    Of course, this is only a introductory example, so if you feel curious about PEAR, visit its site (http://pear.php.net/) and browse the numerous packages available for downloading.

    Having scratched the surface of PEAR_Error objects, let’s leap forward and analyze the last method for handling program failures, in this case by utilizing Boolean flags.

    More PHP Articles
    More By Alejandro Gervasio


       · This first article demonstrates some of the most common approaches for handling...
       · I had expected something about the try catch throw statements as well...Aren't...
       · Hello,Thank you for commenting on my article. Regarding your question, I covered...
       · Boolean flags are a technique I frequently use specifically in writing APIs. I...
       · Hello Lux,Thank you for your feedback on this article. I saw your method about...
     

       

    PHP ARTICLES

    - 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
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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