Php automatically loads the class _ autoload () function. Php automatically loads the class _ autoload () function. many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that the class _ autoload () function has to be automatically loaded in every php script.
Many developers create a PHP source file for each class definition when writing object-oriented applications. A big 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 a software development system, it is impossible to write all classes in one php file. when one php file needs to call the classes declared in another file, you need to introduce this file through include. However, when there are many file projects, it is a headache to include all the files of the required classes one by one, so can we use any classes, what about importing the php file where this class is located? This is what we want to talk about here.Automatic loading class.
In PHP 5, a _ autoload () function can be defined, which is automatically called when trying to use a class that has not been defined. by calling this function, before a PHP error fails, the script engine has the last chance to load the required class. a parameter received by the _ autoload () function is the class name of the class you want to load, therefore, when you create a project, you need to follow certain rules when organizing the file name of the class, it is best to center the class name, or you can add a uniform prefix or suffix to form a file name, for example, xxx_classname.php, classname_xxx.php, and classname. php and so on.
In this example, we try to load the MyClass1 and MyClass2 classes in the MyClass1.php and MyClass2.php files respectively.
The processing method of DiscuzX2.5 is:
Note: __autoload () is designed specifically for the absence of classes !!! Many frameworks use this function to automatically load class files !!!
Articles you may be interested in
- How to set automatic phpmyadmin login and cancel automatic login
- PHP checks browser parameters to prevent SQL injection.
- Detailed usage of the jQuery plug-in (Lazy Load) for delayed image loading
- Some powerful string processing functions forgotten by php
- Php remembers the implementation of the next automatic password login
- Thinkphp automatic verification and automatic filling are invalid solutions
- PHP retrieves the complete url function of the current page, including parameters
- PHP encryption and decryption functions
Many developers create a PHP source file for each class definition when writing object-oriented applications. A big headache is having to put it on every foot...