PHP
  Home arrow PHP arrow Page 3 - Building a Modular Exception Class in PHP 5
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? 
Google.com  
PHP

Building a Modular Exception Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-11-05


    Table of Contents:
  • Building a Modular Exception Class in PHP 5
  • Review: Handling MySQL-related exceptions with two subclasses
  • Centralizing the processing of MySQL-related exceptions
  • Handling MySQL-related exceptions through one single subclass

  • 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


    Building a Modular Exception Class in PHP 5 - Centralizing the processing of MySQL-related exceptions
    ( Page 3 of 4 )

    The next thing I'm going to teach you in this tutorial will be simply how to merge the pair of exception subclasses that I listed earlier into a single subclass that will be responsible for processing all of the exceptions triggered when working with MySQL.

    Please take a look at the signature of this brand new exception subclass, included below:


    // define MySQLException subclass


    class MySQLException extends Exception{

    private $exceptionType;

    public function __construct($message,$code=0){

    // call parent of Exception class

    parent::__construct($message,$code);

    if($code==1){

    $this->exceptionType='MySQLException';

    }

    elseif($code==2){

    $this->exceptionType='ResultException';

    }

    else{

    $this->exceptionType='Unknown Exception';

    }

    }

    public function showMySQLExceptionInfo(){

    return 'Catching '.$this->exceptionType.'...<br />Exception message: '.$this->getMessage().'<br />Source filename of exception: '.$this->getFile().'<br />Source line of exception: '.$this->getLine();

    }

    }

     

    As you can see, the above "MySQLException" subclass now implements the required logic to handle two different types of MySQL-related exceptions. Obviously, this process is performed according to the value assigned to its $code input argument and is easy to follow -- if this parameter has a value of 1, it means that an exception of type "MySQLException" has been triggered by a certain class, while a value of 2 assigned to the parameter in question implies that an exception of type "ResultException" has been launched.

    In both cases, the exceptions are processed in a fairly trivial manner, but I did this purposely, so you can grasp more quickly how to process all of the MySQL-related exceptions via a unique subclass.

    Well, at this point, you hopefully understand how the previous "MySQLException" subclass does its business. Therefore, it's time to create a complete hands-on example where the subclass can be utilized in a handy fashion. The last section of this tutorial will be focused entirely on developing the aforementioned example, in this way concluding this quick overview on subclassing exception with PHP 5.

    Go ahead and read the next few lines. We're almost finished!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek