PHP namespace and automatic loading

不言
Release: 2023-03-24 11:18:01
Original
1730 people have browsed it

This article introduces the namespace and automatic loading of PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Introduction

We talked about automatic loading of classes earlier, and then I was thinking about it.

When we write code, every time we call other classes in another file
We did not write the spl_autoload_register method? So how did we achieve it?

Principle

It turns out that our PHP introduced the concept of namespace in 5.3 (this is one of the reasons why most frameworks do not support versions before 5.3)

Namespace Everyone knows something about it: those who don’t knowGo to the corner and think about it

Namespace is simply a kind of identification, and its main purpose is to solve naming conflicts question.

Just like in daily life, there are many people with the same name. How to distinguish these people? Then you need to add some additional logos.

It seems good to use your work unit as your logo, so you don’t have to worry about the embarrassment of “name collision”.

Namespace classification
  • [ ] Fully qualified namespace

  • [ ] Qualified namespace

new 百度\李彦宏(); // 限定类名new \百度\李彦宏(); // 完全限定类名
Copy after login

When the current namespace is not declared, the qualified class name and the fully qualified class name are equivalent. Because if you don't specify a space, it defaults to global (\).

namespace 谷歌;new 百度\李彦宏(); // 谷歌\百度\李彦宏(实际结果)new \百度\李彦宏(); // 百度\李彦宏(实际结果)
Copy after login

This example shows the difference between using qualified class names and fully qualified class names in namespaces. (Fully qualified class name = current namespace qualified class name)

/* 导入命名空间 */use 百度\李彦宏;new 李彦宏(); // 百度\李彦宏(实际结果)/* 设置别名 */use 百度\李彦宏 AS CEO;new CEO(); // 百度\李彦宏(实际结果)/* 任何情况 */new \百度\李彦宏();// 百度\李彦宏(实际结果)
Copy after login

Using the namespace only prefixes the class name, which is not prone to conflicts, and the system will still not automatically import it.

If the file is not introduced, the system will trigger the __autoload function and pass in the qualified class name as a parameter before throwing the "Class Not Found" error.

So the above examples are all based on the situation that you have manually introduced the relevant files, otherwise the system will throw "Class 'Baidu\Robin Li' not found".

So after introducing the namespace, automatic loading was introduced.

Automatic loading of classes

Automatic loading of classes has been talked about before when we talked about spl_autoload_register. . Today we will talk about another
__autoload()

php's __autoload function is a magic function. Before this function appears, if 100 objects are referenced in a php file, then this file needs Use include or require to introduce 100 class files, which will cause the php file to be extremely large. So there is this __autoload function.

When is the __autoload function called? When the new keyword is used in a php file to instantiate an object, if the class is not defined in this php file, the __autoload function will be triggered. At this time, the php file that defines the class can be introduced, and then, The instantiation was successful. (Note: If the object that needs to be instantiated has found the definition of the class in this file, the __autoload function will not be triggered)

The difference between him and spl_autoload_register is that

When __autoload and spl_autoload_register appear in the file at the same time, spl_autoload_register shall prevail.

Related recommendations:

php download function

PHP printing problem

About PHP namespace

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template