1 Many developers write object-oriented applications, creating a PHP source file for each class definition. A big annoyance is having to write a long list of included files at the beginning of each script (one file per class). 2 In a software development system, it is not possible to write all the classes in a PHP file, and when you need to invoke a class declared in another file in a PHP file, you need to introduce the file through include. However, sometimes, in a large number of projects, to each of the required classes of files are included in, is a very annoying thing, so we can use what class, and then the class where the PHP file import it? That's what we're going to talk about here. Auto-load class. 3In PHP 5, you can define a __autoload () function that will be called automatically when trying to use a class that has not yet been defined, and by calling this function, the scripting engine has a last chance to load the required class before PHP fails, and a parameter that the __autoload () function receives. is the class name of the class you want to load, so when you do the project, when the organization defines the name of the class, you need to follow certain rules, preferably with the class name as the center, or a uniform prefix or suffix to form the file name, such as xxx_classname.php, classname_ Xxx.php and is classname.PHP and so on. 4This example attempts to separate from myclass1.php and MyClass2.loading MyClass1 and MyClass2 classes in PHP files5016<?PHP7028 function__autoload ($classname)903Ten { One04 A require_once $classname. '. php '; -05 - } the06 - -07 - //when the MyClass1 class does not exist, the __autoload () function is called automatically, passing in the parameter "MyClass1" +08 - $obj=NewMyClass1 (); +09 A at10 - //when the MyClass2 class does not exist, the __autoload () function is called automatically, passing in the parameter "MyClass2" -11 - $obj 2=NewMyClass2 (); -12 -?> inNote: __autoload () is specifically designed for the non-existence of the Class!!! Many frameworks use this function to implement automatic loading of class files!!!
1, PHP----automatically load class __autoload () function