When designing object-oriented program development, a PHP source file is usually created for each class definition. When you try to use an undefined class, PHP will report a fatal error. when designing object-oriented program development, a PHP source file is usually created for each class definition. PHP reports a fatal error when you try to use an undefined class. You can use include or require to include the source file of a class. after all, you know the class to be used. If a page requires multiple classes, you have to write a long list of included files at the beginning of the script page to include all the classes required on this page. This process is not only cumbersome, but also error-prone.
PHP provides the automatic class loading function, which can save programming time. When you try to use a class that is not organized by PHP, it will look for a global function of autoload () (not a function declared in the class ). If this function exists, PHP uses a parameter to call it. the parameter is the name of the class.
The following example illustrates how autoload () is used. it assumes that each file in the current directory corresponds to a class. when the script tries to create an instance of a class User, PHP automatically runs the autoload () function. The script assumes that the user class is defined in User. class. php. no matter whether the call is in upper or lower case, PHP will return a lower-case name. Therefore, when creating a project, when organizing the definition of class file names, you need to follow certain rules, be sure to center the class name, or add a uniform prefix or suffix to the file name, for example, classname. class. php, xxx_classname.php, classname_xxx.php, or classname. php, etc. we recommend that you use "classname. class. php format.
The above is the details of the PHP object-oriented code case for automatically loading class__autoload (). For more information, see other related articles in the first PHP community!