PHP
  Home arrow PHP arrow Improving Exception Throwing 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

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


    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
     

       

    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 4 Hosted by Hostway
    Stay green...Green IT