As I stated in the introduction, building a customized exception mechanism with PHP 5 involves two basic steps. First, we must define one or more classes that have the ability to trigger several specific exceptions when something goes wrong. Second, we need to derive the corresponding subclasses from the built-in "Exception" class, which must catch all of these exceptions. As I mentioned before, in the preceding tutorial I showed you how to take the first step; I defined a MySQL-processing class, which was provided with the capacity for launching a few MySQL-related exceptions. If the signature of this particular class doesn't ring any bells for you, here it is: // 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 MySQLException('Invalid number of connection parameters'); } foreach($options as $parameter=>$value){ if(!$value){ throw new MySQLException('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 MySQLException('Error connecting to the server'); } if(!mysql_select_db($this->database,$this->conId)){ throw new MySQLException('Error selecting database'); } } // run query public function query($query){ if(!$this->result=mysql_query($query,$this->conId)){ throw new MySQLException('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'); } } } As you can see, the previous "MySQL" abstraction class will throw some exceptions of type "MySQLException" when failing to connect to the server and selecting a specific database, or when running a SQL query. So far, the way this sample class has been defined is fairly understandable to you, right? The next thing I'm going to teach you will be how to create a customized exception class, whose task will be to catch the aforementioned MySQL-related exceptions. The full details of how this will be done will be covered in the next section, so click on the link below to keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|