HomePHP Improving Exception Throwing when Auto Loading Classes in PHP 5
Improving Exception Throwing when Auto Loading Classes in PHP 5
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!