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!
blog comments powered by Disqus |
|
|
|
|
|
|
|