PHP
  Home arrow PHP arrow Page 3 - Throwing Basic Exceptions When Auto Lo...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Throwing Basic Exceptions When Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2007-12-11

    Table of Contents:
  • Throwing Basic Exceptions When Auto Loading Classes in PHP 5
  • Two examples of the _autoload() PHP 5 function
  • Triggering fatal errors with the _autoload() PHP 5 function
  • Developing a simple mechanism to throw exceptions

  • 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

    Throwing Basic Exceptions When Auto Loading Classes in PHP 5 - Triggering fatal errors with the _autoload() PHP 5 function


    (Page 3 of 4 )

    As I explained in the introduction of this article, if an exception is thrown during the execution of the magic “__autoload()” function, it simply won’t be caught by a “try-catch()” block, as you’d normally expect with other PHP native functions. Most likely, the function in question will trigger a fatal error instead, if for whatever reason, a source class can't be included into client code.

    But this is only a theory, so see for yourself what happens when the file that contains the previous “Result” class is not available to be included into the sample script that you learned before:

    (in this example the “result.php” file is not available)


    function __autoload($className){

    require_once $className.'.php';

    }

    try{

    // 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();

    }


    // Fatal error: __autoload() [function.require]: Failed opening required 'Result.php' (include_path='.;include/path/) in path/to/file


    As you can see, the above hands-on example demonstrates in a nutshell what happens when one of the source classes (in this case, the one called “Result”) required by the previous script isn’t available to be included into client code. In simple words, the script will complain about this problem by triggering a fatal error similar to the one show before.

    Undoubtedly, from a programmer’s point of view a crude fatal error might not be the best option when it comes to handing a failure that happened when trying to include a specific source class. So, is there a more efficient and elegant solution to deal with this type of error when using the “__autoload()” function? Fortunately, the answer to this question is an emphatic yes!

    As you’ll see in the section to come, it’s possible to implement a simple workaround which will make this function throw an exception when, for whatever reason, it fails to load a determined source class.

    To learn how this workaround will be developed, please go ahead and read the last section of this article. We’re almost done!

    More PHP Articles
    More By Alejandro Gervasio


       · Since the "__autoload()" PHP 5 magic function natively isn’t capable of throwing...
       · Although many subjects covered by the PHP section are interesting at first sight,...
       · This is an intro that should have been in the article, instead of all the blah blah...
       · What about the case where the class file exists, but the expected class is not found...
       · In first place I must thank you for the comments on my PHP article, and they’re...
       · Thanks for commenting here. Introductions are completely necessary, because that way...
       · Thank you Anders, for posting your question here. If the class file exists, but for...
       · Mate... I know everyone is bagging you out but I like your style of writing. Its...
       · Thank you for commenting on my PHP article. Hey, I’m glad to know you like my style...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





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