Home  >  Article  >  Backend Development  >  Detailed explanation of __autoload() magic method in PHP

Detailed explanation of __autoload() magic method in PHP

藏色散人
藏色散人Original
2019-07-26 13:51:437952browse

This article will introduce you to the __autoload() magic method in PHP. I hope it will be helpful to friends in need!

__autoload(), trying to load an undefined class

Function:

You can enable it by defining this function Automatic loading of classes.

Before the magic function __autoload() method appeared, if you wanted to instantiate 100 objects in a program file, then you must use include or require to include 100 class files, or you must include these 100 Each class is defined in the same class file - I believe this file will be very large, and then you will be in pain.

But with the __autoload() method, you don’t have to worry about this in the future. This class will automatically load the specified file before you instantiate the object.

Let’s take a look through examples:

Let’s look at the previous methods first:

/** 
 * 文件non_autoload.php 
 */ 
   
require_once('project/class/A.php');  
require_once('project/class/B.php');  
require_once('project/class/C.php');  
   
if (条件A) {  
    $a = new A();  
    $b = new B();  
    $c = new C();  
    // … 业务逻辑  
} else if (条件B) {  
    $a = newA();  
    $b = new B();  
    // … 业务逻辑  
}

Do you see it? Not 100, just 3 seems a bit annoying. And there will be a problem: if the script executes the "Condition B" branch, the file C.php actually does not need to be included. Because any included file, whether used or not, will be compiled by the PHP engine.

If it is not used but compiled, this can be regarded as a waste of resources. Furthermore, if C.php contains D.php, D.php contains E.php. And in most cases, the "Conditional B" branch is executed, which will waste some resources to compile three "useless" files C.php, D.php, and E.php.

So what if you use the __autoload() method?

/** 
 * 文件autoload_demo.php 
 */ 
function  __autoload($className) {  
    $filePath = “project/class/{$className}.php”;  
    if (is_readable($filePath)) {  
        require($filePath);  
    }  
}  
   
if (条件A) {  
    $a = new A();  
    $b = new B();  
    $c = new C();  
    // … 业务逻辑  
} else if (条件B) {  
    $a = newA();  
    $b = new B();  
    // … 业务逻辑  
}

ok, no matter how efficient it is, at least the interface looks much more comfortable, without too many redundant codes.

Let’s take a look at the efficiency here. Let’s analyze it:

When the php engine uses class A for the first time but cannot find it, it will automatically call the __autoload method and add the class The name "A" is passed in as a parameter. Therefore, what we need to do in __autoload() is to find the corresponding file based on the class name and include it. If our method cannot find it, then the PHP engine will report an error.

Note:

You can only use require here, because once it is included, when the php engine encounters class A again, it will not call __autoload, but Using class A directly in memory will not result in multiple inclusions.

Extension:

In fact, with the development of PHP today, `spl_autoload_register` - registering a given function as the implementation of __autoload has been implemented, but this is not included in this article. Within the explanation, if you are interested, you can read the manual on your own.

Recommended PHP practical tutorialhttps://www.php.cn/k.html

The above is the detailed content of Detailed explanation of __autoload() magic method in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn