Home arrow PHP arrow Page 3 - Building a Modular Exception Class in PHP 5

Centralizing the processing of MySQL-related exceptions - PHP

Welcome to the final part of the four-part series “Subclassing exceptions in PHP 5.” By means of a hands-on approach, this series walks you through using inheritance to build finely-tuned exception subclasses. These subclasses can be used to handle, via multiple “try-catch” blocks, several failures that might occur during the execution of a given web application.

TABLE OF CONTENTS:
  1. Building a Modular Exception Class in PHP 5
  2. Review: Handling MySQL-related exceptions with two subclasses
  3. Centralizing the processing of MySQL-related exceptions
  4. Handling MySQL-related exceptions through one single subclass
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
November 05, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: