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

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


    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
     

       

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek