PHP
  Home arrow PHP arrow Page 4 - 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Improving Exception Throwing when Auto Loading Classes in PHP 5 - The Improved Source Code
    (Page 4 of 4 )

    Putting all the pieces together: listing the improved source code of the sample database-driven application

    As I explained in the previous section, below I've included the definitions of all the source files that make up the sample database-driven application you learned in the beginning of this article. This time they include the modified version of the  “__autoload()” function.

    Here are the files:


    (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')){

    return eval("class {$className}{public function __construct(){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();

    }


    Now, having listed all of the source files that comprise the previous database-driven application, suppose that for some reason the file containing the definition of the prior “MySQL” class is simply not available. Based on this situation, when the above “autoload_sample.php” file is parsed by the PHP 5 interpreter, the “__autoload()” magic function will trigger an exception, which will then be caught by the corresponding “catch()” statement and the following error message will be displayed on the browser:


    Class not found!


    Pretty good, right? At this stage I've shown you how to define the “__autoload()” function in order to provide it with the capacity to trigger exceptions that can be completely intercepted by a conventional “try-catch()” block.

    As usual, with many of my articles about PHP development, I encourage you to tweak the code of all the examples developed in this tutorial. Doing this will give you more practice with using the handy “__autoload()” function. Start automatically loading your own PHP 5 source classes.

    Final thoughts

    In this third tutorial of the series I showed you how to tweak the signature of the “__autoload()” PHP 5 magic function in order to give it the capacity to throw exceptions when a particular source class can’t be included into the client code. Of course, the most notable result of improving the definition of this function is that those exceptions can be perfectly caught within a conventional “try-catch()” block, taking advantage of the exceptions mechanism natively provided by PHP 5.

    In the next (and last) part of the series, I’m going to demonstrate how to make the “__autoload()” function even more efficient and elegant by improving the way it throws all eventual exceptions.

    Now that you’re aware of the subject of the next article, you simply can’t miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway