When writing code, you often encounter a headache, that is, to introduce a lot of classes in a page, you need to use the include_once or require_once () function to introduce them one by one. It is acceptable when there are not many introduced content, but if there are more than a dozen or dozens of files that need to be introduced, there are many operations, so don't bother to say, there will also be repeated introductions or missing references. When writing code, you often encounter a headache, that is, to introduce a lot of classes in a page, you need to use the include_once or require_once () function to introduce them one by one. It is acceptable when there are not many introduced content, but if there are more than a dozen or dozens of files that need to be introduced, there are many operations, so don't bother to say, there will also be repeated introductions or missing references.
In PHP5, we can use the _ autoload () method to solve this problem. The __autoload () method can automatically instantiate the class to be used. When the program uses a class but the class has not yet been instantiated, PHP5 will call the _ autoload () method to automatically search for files with the same name as the class in the specified path. If it is found, the program continues; otherwise, an error is reported.
Note:
All other methods work only when they are added inside the class. __autoload () is the only method not added in the class.
As long as a class is used on the page, the class name is automatically passed to this parameter.
For example:
The code of the class file sport. class. php:
type = $type;}public function __toString(){return $this->type;}}?>
Code in the index. php file:
First, do not instantiate the common classes in the class file sport. class. php. Then introduce the class in the class file in the index. php file of php.
The above is the details of the magic method _ autoload () instance (php object oriented advanced tutorial). For more information, see other related articles in the first PHP community!