Automatically load objects:
Many developers create a PHP source file for each class definition when writing object-oriented applications. A big annoyance is having to write a long list of include files at the beginning of each script (one file per class).
In PHP 5, this is no longer necessary. You can define an __autoload function that will be automatically called when trying to use a class that has not yet been defined. By calling this function, the scripting engine has a last chance to load the required classes before PHP fails with an error.
This example attempts to load the MyClass1 and MyClass2 classes from the MyClass1.php and MyClass2.php files respectively.
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
Note:
Exceptions thrown in the __autoload function cannot be caught by the catch statement block and result in a fatal error.