Home arrow PHP arrow Page 2 - Sub Classing Exceptions in PHP 5

Getting started with exceptions with PHP 5 - PHP

If you do any serious programming, whether it's in PHP 5 or some other language, you've needed to know how to handle run time errors and other "exceptional" conditions. You can do this by making your program throw generic exceptions. Or you can unlock the potential of PHP 5 and learn how to create custom exceptions, which is the subject of this four-part series.

TABLE OF CONTENTS:
  1. Sub Classing Exceptions in PHP 5
  2. Getting started with exceptions with PHP 5
  3. Catching MySQL-related exceptions with the PHP 5 Exception class
  4. Modifying the MySQL class's signature to trigger customized exceptions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 15, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: