Automatically load objects:
Many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that you have to write a long list of contained files at the beginning of each script (each class has one file.
In PHP 5, this is no longer needed. You can define a _ autoload function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.
In this example, we try 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:
The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.