PHP
  Home arrow PHP arrow Throwing Basic Exceptions 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

Throwing Basic Exceptions When Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-12-11


    Table of Contents:
  • Throwing Basic Exceptions When Auto Loading Classes in PHP 5
  • Two examples of the _autoload() PHP 5 function
  • Triggering fatal errors with the _autoload() PHP 5 function
  • Developing a simple mechanism to throw exceptions

  • 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


    Throwing Basic Exceptions When Auto Loading Classes in PHP 5
    ( Page 1 of 4 )

    Welcome to the second part of the series that began with “Auto loading classes in PHP 5.” Comprised of four approachable tutorials, this series walks you through the key concepts you need to understand how to automatically include classes with PHP 5, without having to use the popular include functions that are provided by this powerful server-side scripting language.

    Introduction

    As you may have heard, PHP 5 introduces the “__autoload()” magic function within its improved object model. This function can be very useful for loading automatically all of the source classes required by your object-oriented applications. Of course, this feature can be a real time saver if you frequently work with classes during the development of your web projects. So if you’re interested in learning how to put this handy function to work for you with minor hassles, then this series of articles might be what you’re looking for.

    Now that I've introduced you to the main subject of the series, I’m going to spend a few moments refreshing the topics that were covered in the preceding article. This will help you understand much more easily how that tutorial links with this one.

    As you’ll surely recall, during the previous part of the series, I guided you through the process of using the “__autoload()” magic function that comes with PHP 5 to facilitate the inclusion of all the source classes required by a certain PHP application that uses the object-oriented paradigm to work as expected.

    Specifically, in that article I showed you how to implement a simple database-driven application whose primary goal was to fetch some basic data from an “Users” MySQL database table. The application would then display this information on the browser using a couple of MySQL processing classes.

    Of course, as you might recall, the most important thing to highlight here is that I demonstrated by means of two illustrative code samples how to load the respective source classes required by the database-driven application. We used a couple of conventional PHP 5 includes first, and then I showed you how to utilize the capacity of the handy “__autoload()” magic function.

    Undoubtedly, this function makes it really easy to include automatically into client code all the classes that are required by a concrete object-based application. Assuming that the "_autoload()" magic function has been adequately implemented, there's no need to use multiple includes to accomplish this task. Whether you’re including one or ten classes into a specific script, the function will perform the inclusion task neatly.

    So far, I have refreshed the basic concepts surrounding the utilization of the “__autoload()” magic function in the context of a simple database-driven application, which hopefully will provide you with the right pointers to start using it within your own PHP projects as soon as possible. However, it should be noted that the PHP manual says that any exceptions thrown by this function during its execution simply won’t be caught by a “try-catch()” block. This could be rather problematic, particularly if you use to build PHP 5 applications that include exception-based error handling modules.

    Thus, bearing in mind this intrinsic limitation exposed by the “__autoload()” magic function when it comes to triggering exceptions at runtime, in this second article  of the series I’m going to show you some basic workarounds that you can implement to provide this function with the ability to throw exceptions when a determined source class fails to be included.

    So, let’s not waste more time in preliminaries and learn together how to expand the native functionality of the “__autoload()” magic function. Let’s get started now!



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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