Home arrow PHP arrow Page 4 - Exception Handling in PHP

Extending the Exception Class - PHP

In this second part of a two-part series on error and exception handling in PHP, we focus specifically on handling exceptions. This article is excerpted from chapter 8 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Exception Handling in PHP
  2. PHP’s Exception-Handling Implementation
  3. Methods
  4. Extending the Exception Class
  5. Catching Multiple Exceptions
By: Apress Publishing
Rating: starstarstarstarstar / 2
January 04, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Although PHP’s base exception class offers some nifty features, in some situations you’ll likely want to extend the class to allow for additional capabilities. For example, suppose you want to internationalize your application to allow for the translation of error messages. These messages reside in an array located in a separate text file. The extended exception class will read from this flat file, mapping the error code passed into the constructor to the appropriate message (which presumably has been localized to the appropriate language). A sample flat file follows:

1,Could not connect to the database! 2,Incorrect password. Please try again. 3,Username not found.
4,You do not possess adequate privileges to execute this command.

WhenMy_Exceptionis instantiated with a language and an error code, it will read in the appropriate language file, parsing each line into an associative array consisting of the error code and its corresponding message. TheMy_Exceptionclass and a usage example are found in Listing 8-2.

Listing 8-2. The My_Exception Class in Action

class My_Exception extends Exception {

    function __construct($language,$errorcode) {
        $this->language = $language;
        $this->errorcode = $errorcode;
    }

    function getMessageMap() {
        $errors = file("errors/".$this->language.".txt");
        foreach($errors as $error) {
            
list($key,$value) = explode(",",$error,2);
            
$errorArray[$key] = $value;
        }
        return $errorArray[$this->errorcode];
    }

} # end My_Exception

try {
   
throw new My_Exception("english",4);
}
catch (My_Exception $e) {
   
echo $e->getMessageMap();
}



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

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: