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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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