PHP
  Home arrow PHP arrow Page 2 - Improving Exception Throwing when Auto...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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? 
PHP

Improving Exception Throwing when Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2007-12-18

    Table of Contents:
  • Improving Exception Throwing when Auto Loading Classes in PHP 5
  • One Step Backward
  • Improving the Definition
  • The Improved Source Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Improving Exception Throwing when Auto Loading Classes in PHP 5 - One Step Backward
    (Page 2 of 4 )

    Going one step backward: throwing exceptions with the “__autoload()” function independently of a “try-catch()” block

    Before I proceed to teach you how to throw exceptions from inside the corresponding “__autoload()” function, which can then be intercepted correctly by a “try-catch()” block, I’d like to list the full source code of the example I developed in the preceding tutorial of this series, where this functionality wasn’t implemented yet.

    In doing so, it’ll be much easier for you to grasp the small changes that I’m going to introduce to the definition of the “__autoload()” magic function, which will provide it with the capacity to launch the aforementioned exceptions.

    All right, having said that, please take a close look at the following code sample, which implements a basic exception mechanism within the pertinent “__autoload()” function:


    (definition of mysql.php file)


    class MySQL{

    private $host;

    private $user;

    private $password;

    private $database;

    private $connId;

    // constructor

    function __construct($options=array()){

    if(!is_array($options)){

    throw new Exception('Connection options must be an array');

    }

    foreach($options as $option=>$value){

    if(empty($option)){

    throw new Exception('Connection parameter cannot be empty');

    }

    $this->{$option}=$value;

    }

    $this->connectDb();

    }

    // private 'connectDb()' method

    private function connectDb(){

    if(!$this->connId=mysql_connect($this->host,$this->user,$this->password)){

    throw new Exception('Error connecting to MySQL');

    }

    if(!mysql_select_db($this->database,$this->connId)){

    throw new Exception('Error selecting database');

    }

    }

    // public 'query()' method

    public function query($sql){

    if(!$result=mysql_query($sql)){

    throw new Exception('Error running query '.$sql.' '.mysql_error());

    }

    return new Result($this,$result);

    }

    }



    (definition of result.php file)


    class Result{

    private $mysql;

    private $result;

    // constructor

    public function __construct($mysql,$result){

    $this->mysql=$mysql;

    $this->result=$result;

    }

    // public 'fetch()' method

    public function fetch(){

    return mysql_fetch_array($this->result,MYSQL_ASSOC);

    }

    // public 'count()' method

    public function count(){

    if(!$rows=mysql_num_rows($this->result)){

    throw new Exception('Error counting rows');

    }

    return $rows;

    }

    // public 'get_insertId()' method

    public function getInsertId(){

    if(!$insId=mysql_insert_id($this->mysql->connId)){

    throw new Exception('Error getting insert ID');

    }

    return $insId;

    }

    // public 'seek()' method

    public function seek($row){

    if(!int($row)&&$row<0){

    throw new Exception('Invalid row parameter');

    }

    if(!$row=mysql_data_seek($this->mysql->connId,$row)){

    throw new Exception('Error seeking row');

    }

    return $row;

    }

    // public 'getAffectedRows()' method

    public function getAffectedRows(){

    if(!$rows=mysql_affected_rows($this->mysql->connId)){

    throw new Exception('Error counting affected rows');

    }

    return $rows;

    }

    }



    (definition of autoload_example.php file)


     

    try{

    function __autoload($className){

    if(!file_exists($className.'.php')){

    eval("class $className {}");

    throw new Exception('Class not found');

    }

    require_once $className.'.php';

    }

    // connect to MySQL

    $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>'password',
    'database'=>'database'));

    // fetch users from database table

    $result=$db->query('SELECT * FROM users ORDER BY id');

    // display user data

    while($row=$result->fetch()){

    echo 'Id: '.$row['id'].' First Name: '.$row['firstname'].' Last Name: '.$row
    ['lastname'].' Email: '.$row['email'].'<br />';

    }

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you’ll probably recall, the previous hands-on example implements a basic database-driven application that is given the task of fetching some basic data on a few fictional users from a simple MySQL table, which are then displayed on the browser. In addition, I concretely implemented the “__autoload()” function at the beginning of this example so it can be used to include the respective “MySQL” and “Result” classes required by this application in the client code (keep in mind that these reside on different PHP files).

    Now that you've hopefully recalled how the previous database-driven application does its business, suppose that for any reason the file that contains the pertinent “MySQL” class has been removed from the file system.

    Based on this hypothetical and certainly conflictive scenario, when the “autoload_example.php” file is parsed by the PHP 5 interpreter, it will complain loudly because this class simply won’t be available and consequently, the following fatal error will be triggered:


    Fatal error: Function __autoload(MySQL) threw an exception of type 'Exception' in path/to/file


    Much better, but not very elegant, right? In this case, the “__autoload()” function implements a simple workaround that allows it to throw an exception when trying to include the missing “MySQL” class, even when it can’t be properly caught inside the corresponding “try-catch()” block.

    Obviously, from a programmer’s point of view, throwing exceptions that can’t be intercepted by one “try-catch()” block is pointless. In this case, it’s necessary to introduce some additional improvements to the definition of the “__autoload()” magic function, so it can be provided with the capacity to launch exceptions that can be trapped within a “try-catch()” structure.

    Bearing in mind that this issue needs to be fixed quickly, in the upcoming section I’m going to slightly modify the signature of the “__autoload()” function to implement the aforementioned feature.

    To see how these changes will be incorporated into this handy function, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In the last article of the series I discussed how to implement the "__autoload()"...
       · Well, seems that the author simply does not know what a function signature is; he...
       · Thanks for the comments on my PHP article. Unnecessary to mention actually, but I...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway