HomePHP 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.
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!