PHP
  Home arrow PHP arrow Improving Exception Throwing when Auto...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

Improving Exception Throwing when Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2007-12-18

    Table of Contents:
  • Improving Exception Throwing when Auto Loading Classes in PHP 5
  • One Step Backward
  • Improving the Definition
  • The Improved Source Code

  • 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

    Create a Highly Available Database Solution. Advantage Database Server can provide the availability to keep your data safe and ready for your application and users.

    Improving Exception Throwing when Auto Loading Classes in PHP 5
    (Page 1 of 4 )

    This is the third article in the series on how to auto load classes in PHP 5. This article will demonstrate how to trigger exceptions in a way that can be caught by the corresponding "catch()" block.  Please keep reading to find out more.

    The introduction of PHP 5 has rapidly facilitated the development of object-based applications due to the plethora of improvements incorporated into its robust object model. In this way it provides PHP programmers with the features required to get the most out of the object-oriented paradigm.

    Working with objects during the development of a given PHP 5-driven application implies that these must be spawned using their corresponding originating classes. They are frequently loaded into client code by using the "includes" functions provided by this programming language.

    However, when working with medium and large scale applications, manually loading a bunch of classes to perform concrete tasks means that each of them may have to be associated with a “require()/require_once()” statement. At times, this can be an annoying and time-consuming process.

    In response to the above issue, PHP 5 comes packaged with the handy “__autoload()” magic function. When implemented in a clever way, this can be really useful for automatically including all of the source classes required by a given object-based application without having to call the pertinent “require()” PHP functions multiple times.

    If you already read the previous article of this series, this advantage should be quite familiar to you. I explained not only how to utilize the “__autoload()” magic function using a friendly approach, but demonstrated how the implementation of this function can be tweaked to provide it with the capacity to throw exceptions when one source class can’t be properly included into client code for whatever reason.

    Unfortunately, throwing exceptions within the “__autoload()” function implies having to implement some quick and dirty solutions. The most popular is based on declaring the class that the PHP 5 interpreter failed to include, in this case using the “eval()” PHP native function, within the function in question.

    Obviously, there are other approaches available that obligate the “__autoload()” function to throw an exception, but I decided to cover the one that dynamically declares the failed class within the function in question, since I consider it the simplest to implement and learn. Naturally, if you feel inclined to use a different technique to throw exceptions that can be properly intercepted within a “try-catch” block in conjunction with the mentioned function, it’s only a matter of deciding what fits your particular needs.

    All right, at this point you've learned how to launch exceptions when the “__autoload()” magic function is called by the PHP 5 interpreter. However, it’s fair to say that all the practical examples that you saw in the previous installment of the series weren’t capable of triggering these exceptions in a way that can be caught by the “catch()” block.

    Nonetheless, this issue can be fixed with relative ease. That will be the goal of this third article of the series. So if you’re interested in learning how this solution will be implemented, don’t waste any more time and start reading now!

    More PHP Articles
    More By Alejandro Gervasio


       · In the last article of the series I discussed how to implement the "__autoload()"...
       · Well, seems that the author simply does not know what a function signature is; he...
       · Thanks for the comments on my PHP article. Unnecessary to mention actually, but I...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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