PHP
  Home arrow PHP arrow Page 3 - Improving Exception Throwing when Auto Loading Classes 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

Improving Exception Throwing when Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    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:
      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


    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
     

       

    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