PHP
  Home arrow PHP arrow Throwing Basic Exceptions When Auto Lo...
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

Throwing Basic Exceptions 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-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:
      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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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


       · Since the "__autoload()" PHP 5 magic function natively isn’t capable of throwing...
       · Although many subjects covered by the PHP section are interesting at first sight,...
       · This is an intro that should have been in the article, instead of all the blah blah...
       · What about the case where the class file exists, but the expected class is not found...
       · In first place I must thank you for the comments on my PHP article, and they’re...
       · Thanks for commenting here. Introductions are completely necessary, because that way...
       · Thank you Anders, for posting your question here. If the class file exists, but for...
       · Mate... I know everyone is bagging you out but I like your style of writing. Its...
       · Thank you for commenting on my PHP article. Hey, I’m glad to know you like my style...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - 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

     
    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 3 hosted by Hostway