PHP
  Home arrow PHP arrow Page 2 - Sub Classing Exceptions in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Sub Classing Exceptions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-10-15


    Table of Contents:
  • Sub Classing Exceptions in PHP 5
  • Getting started with exceptions with PHP 5
  • Catching MySQL-related exceptions with the PHP 5 Exception class
  • Modifying the MySQL class's signature to trigger customized exceptions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Sub Classing Exceptions in PHP 5 - Getting started with exceptions with PHP 5
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek