PHP
  Home arrow PHP arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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


    Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5 - Boosting the Existing Capacity


    (Page 3 of 4 )


    Boosting up the existing capacity of the “__autoload()” magic function: displaying descriptive error messages

    In accordance with the concepts I explained in the previous section, it’s perfectly possible (and desirable, actually) to modify the current signature of the “__autoload()” magic function so that, whenever a given source class can’t be included into client code, it throws the corresponding exception accompanied by a meaningful error message.

    Given that, to implement this functionality, I’m going to make a slight change to the signature of the pertinent function, so now it looks like this:


    function __autoload($className){

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

    return eval("class {$className}{public function __construct(){throw new
    Exception('Class {$className} not found!');}}");

    }

    require_once $className.'.php';

    }


    Not too difficult to grasp, right? As you can see, now all of the exceptions triggered by the above “__autoload()” function will indicate which source class wasn’t loaded at runtime, since the name of the troubling class is simply passed to the constructor of the built-in Exception class.

    To illustrate even more clearly how the “__autoload()” function works after introducing the previous modification, below I included an example that displays a “MySQL class not found” error message, when this class can’t be automatically loaded.

    The corresponding code sample is as follows:


    try{

    function __autoload($className){

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

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

    echo $e->getMessage();

    }


    As I said before, if you test the above example on your own computer system and purposely remove the file that contains the pertinent “MySQL” class, then the “__autoload()” function will trigger an exception with the following error message: “Class MySQL not found!”

    Indeed, this message is much more meaningful than a raw “Class not found” text, because it gives you a clear idea of which source class couldn’t be correctly loaded by the “__autoload()” function during the execution of a given application.

    Okay, at this stage you hopefully learned how to tweak the signature of this handy function to display more descriptive error messages when something goes wrong. However, there are many other approaches that can be implemented to get the same results that you saw earlier.

    Nevertheless, in this article I’m going to show you only one more of these additional approaches, which as you’ll see in a moment, will be based on working with the so-called exceptions sub classing.

    Do you want to see how this technique will be developed? Click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · 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

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





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