Home arrow PHP arrow Page 4 - Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5

Developing a Final Example - PHP

If you’re a PHP developer who wants to learn how to load automatically into client code all of the source classes required by your object-oriented applications, keep reading. In this series of articles, you’ll learn how to implement the magic “__autoload()” function that comes integrated with PHP 5. With this function, you'll be able to build scripts that can include automatically all of the classes needed by a given application, without using multiple “require()/require_once()” PHP statements.

  1. Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5
  2. Establishing a Reference Point
  3. Boosting the Existing Capacity
  4. Developing a Final Example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 26, 2007

print this article



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:


// define custom 'AutoloadException' class (extends built-in Exception class)

class AutoloadException extends Exception{}

function __autoload($className){


return eval("class {$className}{public function __construct(){throw new
AutoloadException('Class {$className} not found!');}}");


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


echo 'Id: '.$row['id'].' First Name: '.$row['firstname'].' Last Name: '.$row
['lastname'].' Email: '.$row['email'].'<br />';



// 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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: