Developing a final example: displaying meaningful error messages with exception subclasses As you possibly know, there are other techniques that can be implemented in conjunction with the “__autoload()” function, to display indicative error messages when a particular source class can’t be loaded by the function in question. In this case, I’m going to show you how to create custom “autoloading” exceptions, which obviously will be thrown by the “__autoload()” function. Basically, this procedure is based upon deriving a subclass from the base Exception, which will be used when applicable by the mentioned function. If this concept sounds rather confusing to you, the following example should dissipate any possible doubts, so have a close look at it, please: try{ // define custom 'AutoloadException' class (extends built-in Exception class) class AutoloadException extends Exception{} function __autoload($className){ if(!file_exists($className.'.php')){ return eval("class {$className}{public function __construct(){throw new } require_once $className.'.php'; } // connect to MySQL $db=new MySQL(array // fetch users from database table $result=$db->query('SELECT * FROM users ORDER BY id'); // display user data while($row=$result->fetch()){ echo 'Id: '.$row['id'].' First Name: '.$row['firstname'].' Last Name: '.$row } } // catch Autoload Exceptions catch(AutoloadException $e) { echo $e->getMessage(); } // catch generic exceptions catch(Exception $e) { echo $e->getMessage(); } As you can see, the above example first creates a new “AutoloadException” class from the “Exception” parent, and then implements the pertinent “__autoload()” function in such a way that it will trigger this specific type of exception when a source class fails to be included into client code. In addition, you should notice the existence of two different “catch()” statements. The first one is responsible for intercepting only “auto load” exceptions, and the second one is tasked with catching only regulars ones. And with this final example, we have finished exploring the capacities of the “__autoload()” magic function. Logically, from this point onward, it’s up to you improve your existing skills for using this handy function in conjunction with your own object-based PHP 5 applications. Final thoughts It’s hard to believe, but we’ve come to the end of this series. As you saw in the different parts of this tutorial, the “__autoload()” magic function that comes bundled with PHP 5 can be really useful for including automatically all of the source classes required by a certain object-oriented application, without our having to be concerned as to how this task is performed behind scenes by the PHP 5 interpreter. Also, I provided you with several examples of how to incorporate into this function the capacity for throwing exceptions that can be easily caught by a conventional “try-catch()” block. So with all this distilled material in your hands, you can start right now using this handy function within your own PHP 5-driven application. See you in the next PHP development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|