Home arrow PHP arrow Throwing Basic Exceptions When Auto Loading Classes in PHP 5

Throwing Basic Exceptions When Auto Loading Classes in PHP 5

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.

TABLE OF CONTENTS:
  1. Throwing Basic Exceptions When Auto Loading Classes in PHP 5
  2. Two examples of the _autoload() PHP 5 function
  3. Triggering fatal errors with the _autoload() PHP 5 function
  4. Developing a simple mechanism to throw exceptions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: