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 calling order of namespaces in php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you the order of calling namespaces in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

First of all, let's look at a small example.

If we take a closer look at the small example above, what can we observe? Dangdang, let's give the answer now.

When a function is called, if we only write "foo ()", the function in the namespace is called first, and then the global function is called; but if it is "\ foo ()", only the global function is called.

When the class is applied, if we write "new B ();", an object of class "B" defined in the namespace will be created, but if it is not found, try to automatically load class "A\ B".

When calling a static method or namespace function in another namespace, how do we write "B\ foo ()", which means that we will call the function "foo ()" in the namespace; but if it says "B::foo ();" it is different. He first calls the function "foo ()" in the namespace, but if it is not found, he tries to load the class "A\ B" automatically.

In the case of static methods or functions in the current namespace, how do we write "A\ B::foo ();", which means that we will call the "foo" method of class "B" defined in namespace "A\ A" and automatically load class "A\ A\ B" if it is not found.

Now let's sum it up.

Calls to fully qualified functions, classes, and constants will be parsed at compile time. For example, new\ a\ B resolves to class a\ B.

All unqualified and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace A\ B\ C is imported to C, the call to C\ D\ e () is converted to A\ B\ C\ D\ e ().

All unqualified and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace a\ B\ C is imported as C, the call to C\ D\ e () is converted to a\ B\ C\ D\ e ().

The unqualified class name is converted at compile time according to the current import rule (replacing the short import name with the full name). For example, if the namespace a\ B\ C is imported as C, the new C () is converted to the new a\ B\ C ().

In the namespace (for example, a\ b), function calls to unqualified names are resolved at run time. For example, the call to the function foo () parses as follows:

Look for a function named A\ B\ foo () in the current namespace

Try to find and call the function foo () in the global (global) space.

Calls to unqualified names or qualified name classes (non-fully qualified names) in namespaces such as a\ b are resolved at run time. The following is the parsing process of calling new c () and new d\ e (): parsing new c ():

Look for class A\ B\ C in the current namespace.

Attempt to automate class A\ B\ C.

Resolution of new D\ E ():

Add the current namespace name before the class name to: a\ B\ D\ E, and then look for the class.

Try automounting class A\ B\ D\ E.

In order to reference a global class in the global namespace, you must use the fully qualified name new\ C ().

These are all the contents of the article "the order in which namespaces are called in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report