Programming Basics

  Home arrow Programming Basics arrow More Exception Handling with PHP
PROGRAMMING BASICS

More Exception Handling with PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-09-14

    Table of Contents:
  • More Exception Handling with PHP
  • Catching Multiple Exceptions

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    More Exception Handling with PHP


    (Page 1 of 2 )

    In this conclusion to a five-part article series on error and exception handling in PHP, you'll learn how to extend the exception class, and how to catch multiple exceptions. This article is exceprted from chapter eight of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

    Extending the Exception Class

    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.

    WhenMyExceptionis instantiated with a language and 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. TheMyExceptionclass and a usage example are found in Listing 8-2.

    Listing 8-2. The MyException Class in Action

    class MyException 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 MyException

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

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap