Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of automatic loading of classes in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the principle of automatic loading of classes in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Automatic loading of classes in PHP

In the past, we have learned the principle of Composer auto-loading, which actually takes advantage of the class auto-loading feature in PHP. There are links to this series of articles at the end of the article.

The automatic loading of classes in PHP mainly depends on the _ _ autoload () and spl_autoload_register () methods. Today we will take a brief look at the use of these two methods.

_ _ autoload ()

As a magic method that is about to be eliminated, we just need to know. If you use this method in PHP7, an outdated warning will be reported, and we will be advised to 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, nor does it include or require from other files, so it automatically enters the magic method _ _ autoload (). All we need to do in the _ _ autoload () method is to include the file where the class is located.

Spl_autoload_register ()

At present, this method has replaced the function of automatically loading classes in the above magic method. It is a method in the spl extension library, and the spl extension library is now integrated into PHP by default, and you can safely use it directly.

The advantage of spl_autoload_register () over _ _ autoload () is that it can register a _ _ autoload () and implement and maintain a _ _ autoload () queue. There used to be only one _ _ autoload () method in a file, but now all you have is a queue.

Instead of writing all the loading code in one _ _ autoload () method, you can use multiple spl_autoload_register () to load each class individually.

Spl_autoload_register (function ($name) {

Include _ _ DIR__. '/ autoload/'. $name. '.class.php'

});

$autoA = new AutoA ()

Var_dump ($autoA)

Using include or include_once

In automatic loading, we only need to use include, and the class does not load 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 output only once in the case of multiple instantiation of the class. So there is no need to worry about the problem that class files will be loaded repeatedly. And when using composer in a large framework, a lot of classes will be loaded, and the _ once method will also cause efficiency problems.

This is the end of the introduction of "the principle of automatic loading of classes in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report