Go deep into the PHPautoload mechanism. When using the php oo mode to develop a system, we usually get used to storing the implementation of each class in a separate file, which makes it easy to reuse classes, at the same time, in the future, when we use PHP's OO mode to develop a system, we usually get used to storing the implementation of each class in a separate file, which makes it easy to reuse classes, at the same time, it will be very convenient for future maintenance. This is also one of the basic ideas of OO design. To use a class before PHP5, you only need to use
Include/requireInclude it.
The following is an example:
The code is as follows:
/* Person. class. php */
Class Person {
Var $ name, $ age;
Function _ construct ($ name, $ age)
{
$ This-> name = $ name;
$ This-> age = $ age;
}
}
?>
/* No_autoload.php */
Require_once ("Person. class. php ");
$ Person = new Person ("Altair", 6 );
Var_dump ($ person );
?>
In this example, the no-autoload.php file needs to use the Person class, which uses require_once to include it, and then you can directly use the Person class to instantiate an object.
However, as the project scale continues to expand, using this method will bring about some implicit problems: if a php file needs to use many other classes, a lot of require/include statements are required, this may cause omissions or include unnecessary class files. If a large number of files require other classes, it is a nightmare to ensure that each file contains the correct class files.
PHP5 provides a solution for this problem, which is the automatic loading mechanism of the class. The autoload mechanism makes it possible for PHP programs to automatically include class files when using classes, rather than include all class files at the beginning. this mechanism is also called lazy loading.
The following is an example of using the autoload mechanism to load the Person class:
The code is as follows:
/* Autoload. php */
Function _ autoload ($ classname ){
Require_once ($ classname. "class. php ");
}
$ Person = new Person ("Altair", 6 );
Var_dump ($ person );
?>
...