Home arrow PHP arrow Page 4 - Error Handling in PHP: Coding Defensively

The PEAR way: looking at the PEAR_Error object - PHP

As with any programming language, when you code in PHP, it helps immensely if you set up your applications to handle errors gracefully. This article explores some of the most common error checking methods available in PHP, and provides hands-on examples that use different error handling methods.

TABLE OF CONTENTS:
  1. Error Handling in PHP: Coding Defensively
  2. Killing your scripts: basic error handling with the “die()” statement
  3. Triggering errors: more efficient error handling with the “trigger_error()” function
  4. The PEAR way: looking at the PEAR_Error object
  5. True and false: handling errors with Boolean flags
By: Alejandro Gervasio
Rating: starstarstarstarstar / 52
January 11, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: