Home  >  Article  >  Backend Development  >  Detailed explanation of automatic loading of classes in PHP

Detailed explanation of automatic loading of classes in PHP

藏色散人
藏色散人forward
2021-02-15 09:13:163263browse

Automatic loading of classes in PHP

Before, we have learned Through the principle of Composer's automatic loading, it actually takes advantage of the automatic class loading feature in PHP.

The automatic loading of classes in PHP mainly relies on the two methods __autoload() and spl_autoload_register().

Today we will briefly look at the use of these two methods.

__autoload()

As a magic method that is about to be eliminated, we just need to understand it. If this method is used in PHP7, an outdated warning will be reported, and the system will recommend that we use the spl_autoload_register() method.

function __autoload($name){
    include __DIR__ . '/autoload/' . $name . '.class.php';
}

$autoA = new AutoA();
var_dump($autoA);

When we instantiate the AutoA class, the current file does not have this class, and it does not include or require from other files. At this time, it will automatically enter the magic method __autoload(). In the __autoload() method, we only need to include the file where this class is located.

spl_autoload_register()

This method has currently replaced the function of automatically loading classes by the above magic method. It is a method in the spl extension library. The spl extension library has now been integrated into PHP by default. You can use it directly with confidence.

The advantage of spl_autoload_register() over __autoload() is that it can register an __autoload(), and implements and maintains an __autoload() queue. Originally there could only be one __autoload() method in a file, but now, what you have is a queue.

In this way, you do not need to write all loading code in one __autoload() method, but can use multiple spl_autoload_register() to load each class separately.

spl_autoload_register(function($name){
    include __DIR__ . '/autoload/' . $name . '.class.php';
});

$autoA = new AutoA();
var_dump($autoA);

Reference: In-depth study of Composer principles (2)

Use include or include_once

In automatic loading, we only need to use include , the class will not be loaded repeatedly.

spl_autoload_register(function($name){
    include __DIR__ . '/autoload/' . $name . '.class.php';
    echo $name, PHP_EOL;
});

$autoA = new AutoA();
var_dump($autoA);

$autoA = new AutoA();
var_dump($autoA);

$autoA = new AutoA();
var_dump($autoA);

$autoB = new AutoB();
var_dump($autoB);

From the code, we can see that $name is only output once when the class is instantiated multiple times. So there is no need to worry about the problem of repeated loading of class files. Moreover, when using composer in a large framework, a lot of classes will be loaded, and the _once method will also cause efficiency problems.

The above is the detailed content of Detailed explanation of automatic loading of classes in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete