How to Use PHP Auto Load Class File Function __autoload

  • 2021-12-04 18:17:49
  • OfStack

When developing object-oriented applications, it is often necessary to establish an PHP source file for each class definition. One big annoyance of this approach is having to write a long list of included files at the beginning of each script (1 file per class).

In the system developed by PHP, when a class declared in another PHP file needs to be called in one file, this file needs to be introduced through include or require. However, sometimes, in a project with many files, it is a headache to include all the files of the required classes, so can we import the php file where this class is located when we use it? This is what we are going to talk about auto-loading classes here.

In PHP 5, you can define 1 __autoload() Function, which is automatically called when trying to use a class that has not yet been defined, By calling this function, Script engine in PHP error before the failure of the last opportunity to load the required class, __autoload () function to receive a parameter, is the class name of the class you want to load, so you do the project, in the organization of the definition of the class file name, need to be in accordance with the rules of 1, it is best to class name as the center, you can also add a unified 1 prefix or suffix to form a file name, such as xxx_classname.php , classname_xxx.php And that is classname.php Wait.

Let's use __autoload under 1 as an example, loading the ClassA and ClassB classes from the ClassA. php and ClassB. php files, respectively


<?php
// Definition 1 Category ClassA The file name is ClassA.php
class ClassA{
 public function __construct(){
 echo "ClassA load success!";
 }
}
?>

<?php
// Definition 1 Category ClassB The file name is ClassB.php , ClassB Inheritance ClassA
class 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!';
 }
}
//ClassA Class does not exist, automatically call the __autoload() Function, passing in arguments " ClassA " 
$obj = new ClassA();
//ClassB Class does not exist, automatically call the __autoload() Function, passing in arguments " ClassB " 
$obj2 = new ClassB();
?>

From the above example, we found that when using ClassA and ClassB, we didn't manually import ClassA. php and ClassB. php files, but we could use these two classes normally, which shows how easy __autoload is.

However, when using __autoload, we should also pay attention to some problems, such as the above ClassB class, which inherits ClassA class. If ClassA and ClassB are not in the same directory, errors will occur. Therefore, it is recommended to put all classes with extends relationship in the same file directory, or manually include inherited classes in the file when instantiating an inherited class. Another point is that when using auto-load function, 1 must pay attention to the corresponding relationship between class name and file name.

Summarize


Related articles: