automatic loading of classesMeaning:
when a line of code needs a class,the internal mechanism of PHP can be "automatically load the class file" to meet the need for a class of this line needs.
When do I need a class?
1,whennew is an object;
2, when using a static method of a class;
3, when defining a class (B) and using another class (A) as the parent class;
Conditions and Requirements
1, when a class is required, a function is called automatically (by default , __autoload), and the name of the required class is passed in.
2, a class should be saved to a separate "class file": That is, only the definition of the class, no other code;
3, it is customary for a class file to be named with a certain "rule", usually: the class name . class.php
4, usually, we need to store the various classes in some specific directory, to facilitate the identification of its location!
5, in the auto-loaded function, "full" uses the passed-in class name, to build a suitable file path and load;
Custom Auto-load functions:
just now,the __autoload () function is an auto-load function inside the system, and we just define its function body.
But:
We can use more functions (custom) to achieve more flexible auto-loading!
The basic mode is:
Spl_autoload_register (" function 1"); declaring "function 1" as an auto-loading function;
Spl_autoload_register (" function 2"); declaring "function 2" also acts as an auto-loading function;
.........
then, define these functions as you would define the __autoload () function:
function 1 ($class _name) {
//.......
}
function 2 ($class _name) {
//.......
}
.............
In this way, the system will call these auto-load functions at once to load the required classes until the loading is successful!
PHP Object-oriented automatic loading