Before I start teaching you a simple way to create customized exceptions with PHP 5, let me show you an introductory example. In this case, the built-in “Exception” class is used to handle certain errors triggered by a pair of MySQL processing classes. The respective signatures of these PHP 5 classes are coded as follows: // define 'MySQL' class class MySQL{ private $conId; private $host; private $user; private $password; private $database; private $result; const OPTIONS=4; public function __construct($options=array()){ if(count($options)!=self::OPTIONS){ throw new Exception('Invalid number of connection parameters'); } foreach($options as $parameter=>$value){ if(!$value){ throw new Exception('Invalid parameter '.$parameter); } $this->{$parameter}=$value; } $this->connectDB(); } // connect to MySQL private function connectDB(){ if(!$this->conId=mysql_connect($this->host,$this->user,$this->password)){ throw new Exception('Error connecting to the server'); } if(!mysql_select_db($this->database,$this->conId)){ throw new Exception('Error selecting database'); } } // run query public function query($query){ if(!$this->result=mysql_query($query,$this->conId)){ throw new Exception('Error performing query '.$query); } return new Result($this,$this->result); } } // define 'Result' class class Result { private $mysql; private $result; public function __construct($mysql,$result){ $this->mysql=$mysql; $this->result=$result; } // fetch row public function fetchRow(){ if(!$row=mysql_fetch_assoc($this->result)){ return false; } return $row; } // count rows public function countRows(){ if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); } return $rows; } // count affected rows public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected rows'); } return $rows; } // get ID of last-inserted row public function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } // seek row public function seekRow($row=0){ if(!is_int($row)||$row<0){ throw new Exception('Invalid result set offset'); } if(!mysql_data_seek($this->result,$row)){ throw new Exception('Error seeking data'); } } } If you’ve ever had the chance to read some of my previous PHP-related articles, published here on the prestigious Developer Shed Network, then it’s highly probable that the two previous MySQL handling classes are pretty familiar to you, since I’ve used them before. As you can see, the classes are very easy to grasp; they perform some common tasks related to MySQL, such as connecting to the server and selecting a database, performing queries and handling result sets, and so forth. However, apart from my explanation of how these classes work, I’d like you to pay attention to the way the some of their methods handle certain error conditions. In this specific case, if (for whatever reason) it’s not possible to establish a connection to MySQL, or a specified database can’t be selected, or a query fails to be executed, then a generic exception will be thrown, by using the built-in “Exception” class. So far, there's nothing unexpected, right? At this point, I coded a couple of MySQL-related classes that are capable of handling certain application errors by means of a few generic exceptions. Nonetheless, this initial hands-on example would be rather incomplete if I don’t show you how these exceptions can be properly caught within a “try-catch” block. Therefore, in the section to come I’m going to demonstrate how the native exception mechanism of PHP 5 can be utilized to handle all of the potential errors triggered by the previous MySQL-processing classes. To see how this process will be achieved, please jump ahead and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|