SoFunction
Updated on 2025-03-04

How to use PHP automatic loading of class file function__autoload

When developing object-oriented applications, you often need to create a PHP source file for the definition of each class. A big annoyance caused by this approach is having to write a long list of files at the beginning of each script (one file per class).

In PHP developed systems, when a class declared in another PHP file needs to be called in one file, the file needs to be introduced through include or require. However, sometimes, in projects with many files, it is a headache to include all the required class files one by one, so can we import the php file where this class is located when using any class? This is what we are going to talk about here to automatically load the class.

In PHP 5, you can define a__autoload()The function will be automatically called when trying to use a class that has not yet been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails. A parameter received by the __autoload() function is the class name of the class you want to load. Therefore, when you are doing a project, when you organize the file name of the class, you need to follow certain rules. It is best to focus on the class name, or add a unified prefix or suffix to form a file name, for example,xxx_classname.phpclassname_xxx.phpAnd that's itetc.

Let's use an example to illustrate the usage of __autoload. The following loads the ClassA and ClassB classes from and files respectively

<?php
//Define a class ClassA with the file nameclass ClassA{
 public function __construct(){
 echo "ClassA load success!";
 }
}
?>
<?php
//Define a class ClassB, the file name is, ClassB inherits ClassAclass ClassB extends ClassA {
 public function __construct(){
 echo "ClassB load success!";
 }
}
?>
<?php
function __autoload($classname)
{
 $classpath="./".$classname.'.php';
 if(file_exists($classpath)){
 require_once($classpath);
 }
 else{
 echo 'class file'.$classpath.'not found!';
 }
}
// When the ClassA class does not exist, the __autoload() function is automatically called and the parameter "ClassA" is passed in$obj = new ClassA();
// When the ClassB class does not exist, the __autoload() function is automatically called and the parameter "ClassB" is passed in.$obj2 = new ClassB();
?>

By following the above example, we found that when using ClassA and ClassB, we did not manually import and file, but we could use these two classes normally, which shows how easy it is to use __autoload.

However, when using __autoload, you should also pay attention to some issues. For example, the ClassB class above inherits the ClassA class. If ClassA and ClassB are not in the same directory, an error will occur. Therefore, it is recommended to place all classes with extends relationships in the same file directory, or manually include the inherited class in the file when instantiating an inherited class. Another point is that when using the automatic loading function, you must pay attention to the correspondence between the class name and the file name.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links