PHP
  Home arrow PHP arrow Page 4 - Displaying Meaningful Error Messages w...
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

Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-12-26

    Table of Contents:
  • Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5
  • Establishing a Reference Point
  • Boosting the Existing Capacity
  • Developing a Final Example

  • 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

    Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5 - Developing a Final Example


    (Page 4 of 4 )


    Developing a final example: displaying meaningful error messages with exception subclasses

    As you possibly know, there are other techniques that can be implemented in conjunction with the “__autoload()” function, to display indicative error messages when a particular source class can’t be loaded by the function in question.

    In this case, I’m going to show you how to create custom “autoloading” exceptions, which obviously will be thrown by the “__autoload()” function. Basically, this procedure is based upon deriving a subclass from the base Exception, which will be used when applicable by the mentioned function.

    If this concept sounds rather confusing to you, the following example should dissipate any possible doubts, so have a close look at it, please:


    try{

    // define custom 'AutoloadException' class (extends built-in Exception class)


    class AutoloadException extends Exception{}


    function __autoload($className){

    if(!file_exists($className.'.php')){

    return eval("class {$className}{public function __construct(){throw new
    AutoloadException('Class {$className} 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 Autoload Exceptions

    catch(AutoloadException $e) {

    echo $e->getMessage();

    }

    // catch generic exceptions

    catch(Exception $e) {

    echo $e->getMessage();

    }


    As you can see, the above example first creates a new “AutoloadException” class from the “Exception” parent, and then implements the pertinent “__autoload()” function in such a way that it will trigger this specific type of exception when a source class fails to be included into client code.

    In addition, you should notice the existence of two different “catch()” statements. The first one is responsible for intercepting only “auto load” exceptions, and the second one is tasked with catching only regulars ones.

    And with this final example, we have finished exploring the capacities of the “__autoload()” magic function. Logically, from this point onward, it’s up to you improve your existing skills for using this handy function in conjunction with your own object-based PHP 5 applications.

    Final thoughts

    It’s hard to believe, but we’ve come to the end of this series. As you saw in the different parts of this tutorial, the “__autoload()” magic function that comes bundled with PHP 5 can be really useful for including automatically all of the source classes required by a certain object-oriented application, without our having to be concerned as to how this task is performed behind scenes by the PHP 5 interpreter.

    Also, I provided you with several examples of how to incorporate into this function the capacity for throwing exceptions that can be easily caught by a conventional “try-catch()” block. So with all this distilled material in your hands, you can start right now using this handy function within your own PHP 5-driven application.

    See you in the next PHP development tutorial!


    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 this final part of the series, the implementation of the "__autoload()" magic...
       · 1. interesting solution but if you are using several different libraries with it's...
       · Thank you for posting your useful comments on my PHP article. Yes, as some other...
     

       

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