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

    Dell PowerEdge Servers

    Improving Exception Throwing when Auto Loading Classes in PHP 5 - Improving the Definition
    (Page 3 of 4 )


    Improving the definition of the “__autoload()” function: throwing exceptions that can be caught by a “try-catch” block


    In consonance with the concepts deployed in the prior section, it’s necessary to modify the initial definition of the “__autoload()” magic function so that it can throw an exception when it fails to include a determined source class into the code. In this case, the exception should be caught by a “try-catch()” block. It's an extremely handy technique for taking advantage of the built-in exception mechanism provided by PHP 5.

    So, keeping this in mind, below I listed the improved definition of the “__autoload()” function, which, at this point, is capable of triggering exceptions that can be easily caught by a “try-catch()” block.

    Having said that, the modified signature of this handy function is as follows:


    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';

    }


    See how easy it is to change the definition of the “__autoload()” PHP 5 function to make it throw exceptions that can be correctly intercepted within a “try-catch()” structure? I guess you do! In this particular case, I implemented the function in such a way that when it fails to include a source class, it first declares this class, then an exception is triggered by the corresponding constructor, and finally, the class’ code is returned to the program’s flow.

    Of course, I should say that this workaround is quite dirty and not very elegant. But you should keep in mind that I’m trying to implement a feature that isn’t supported natively by the "__autoload()” function (i.e. triggering exceptions that can be caught within a “try-catch()” block).

    So far, so good. At this stage I've demonstrated how to modify the signature of the “__autoload()” function in order to add to it a decent exception support. However, if you’re anything like me, then you may want to see for yourself whether this improved incarnation of the function really works as expected.

    Thus, in the last section of this tutorial I’m going to create a fully functional example that will demonstrate how the “__autoload()” function is now capable of throwing exceptions that can be intercepted within a “try-catch()” block.

    To learn how this example will be developed, go ahead and read the next few lines. They’re just one click away.

    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 6 hosted by Hostway