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

Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT