PHP
  Home arrow PHP arrow Page 5 - Error Handling in PHP: Coding Defensively
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

Error Handling in PHP: Coding Defensively
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 41
    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:
      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


    Error Handling in PHP: Coding Defensively - True and false: handling errors with Boolean flags
    ( Page 5 of 5 )

    The last error handling method we'll review here is perhaps the most ancient. Boolean flags have been used from the very beginning of many programming languages, and PHP isn’t an exception. While they’re quite easy to implement, the major drawback is that they’re not very informative about the error that occurred and its context. Here’s a crude (and rather inefficient) implementation of the “FileReader” class, which uses Boolean flags:

    class FileReader{
      var $file;
      var $fileDir='fileDir/';
      function FileReader($file){
        if(!@file_exists("{$this->fileDir}{$file}.php")){
          return false;
        }
        $this->file=$file;
      }
      function getContent(){
        if(!@$content=file_get_contents("{$this->fileDir}{$this->file}.php")){
          return false;
        }
        return $content;
      }
    }

    Considering the definition for the above example, class errors might be handled as follows:

    $fr=new FileReader('inexistent_file');
    if(!$fr->getContent()){
      die('Unable to read file contents');
    }
    else{
      echo $fr->getContent();
    }

     

    In the example, I’ve deliberately used the “@” error suppression operator, in order to avoid the complaints of the PHP interpreter and return a false value (or 0 or –1) when a failure happens. At first glance, you can see the inefficiency of this method, as well as its limited flexibility. However, this approach has proven to be quite successful in procedural applications, or when client code is capable of handling simple errors without overly polluting the whole application.

    At this point, I’ve explored the pros and cons of common error handling approaches in PHP 4. Probably you’ll want to add a few more methods to my generic list, but in general terms, this is what you’ll need to use in most cases. Definitely, in large web applications, a set of error manipulating classes is preferred, so you can handle errors through a centralized point. However, the “trigger_error()/set_error_handler()” combination may suit the needs of small projects, so it’s worth considering.

    To wrap up

    In this first tutorial, I’ve hopefully covered the most common techniques for handling failures within PHP 4 applications. Over the second (and last part) of this series, I’ll be focusing attention on exceptions in PHP 5. As you’ll see, exceptions provide a powerful mechanism for handling errors through a centralized point, and offer a nice level of information on raised errors and their context. Want to know more? You’ll have to wait for the last part!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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