In the previous article, we learned about accessing internal elements of the namespace. If necessary, please read "The editor will show you how to access the internal elements of the namespace (php version)". This time we will introduce the order of namespaces to you, you can refer to it if necessary.
First let us look at a small example.
<?php namespace A; use B\D, C\E as F; // 函数调用 foo(); // 首先尝试调用定义在命名空间"A"中的函数foo() // 再尝试调用全局函数 "foo" \foo(); // 调用全局空间函数 "foo" my\foo(); // 调用定义在命名空间"A\my"中函数 "foo" F(); // 首先尝试调用定义在命名空间"A"中的函数 "F" // 再尝试调用全局函数 "F" // 类引用 new B(); // 创建命名空间 "A" 中定义的类 "B" 的一个对象 // 如果未找到,则尝试自动装载类 "A\B" new D(); // 使用导入规则,创建命名空间 "B" 中定义的类 "D" 的一个对象 // 如果未找到,则尝试自动装载类 "B\D" new F(); // 使用导入规则,创建命名空间 "C" 中定义的类 "E" 的一个对象 // 如果未找到,则尝试自动装载类 "C\E" new \B(); // 创建定义在全局空间中的类 "B" 的一个对象 // 如果未发现,则尝试自动装载类 "B" new \D(); // 创建定义在全局空间中的类 "D" 的一个对象 // 如果未发现,则尝试自动装载类 "D" new \F(); // 创建定义在全局空间中的类 "F" 的一个对象 // 如果未发现,则尝试自动装载类 "F" // 调用另一个命名空间中的静态方法或命名空间函数 B\foo(); // 调用命名空间 "A\B" 中函数 "foo" B::foo(); // 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法 // 如果未找到类 "A\B" ,则尝试自动装载类 "A\B" D::foo(); // 使用导入规则,调用命名空间 "B" 中定义的类 "D" 的 "foo" 方法 // 如果类 "B\D" 未找到,则尝试自动装载类 "B\D" \B\foo(); // 调用命名空间 "B" 中的函数 "foo" \B::foo(); // 调用全局空间中的类 "B" 的 "foo" 方法 // 如果类 "B" 未找到,则尝试自动装载类 "B" // 当前命名空间中的静态方法或函数 A\B::foo(); // 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法 // 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B" \A\B::foo(); // 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法 // 如果类 "A\B" 未找到,则尝试自动装载类 "A\B" ?>
Looking carefully at the above small example, what can we observe? Dangdang, we will give the answer now.
When calling a function, if we only write "foo()
", the function in the namespace will be called first, and then the global function will be called; but if it is "\foo()
", this only calls the global function.
When applying a class, if we write "new B();
", an object of class "B" defined in the namespace will be created, but if it is not found , then try to automatically load class "A\B
".
When calling a static method or namespace function in another namespace, we write "B\foo()
", which indicates that we will call the namespace function Function "foo()
"; but if you write "B::foo();
" it is different. He first calls the function "foo() in the namespace ", but if not found, an attempt is made to autoload class "A\B".
When we write a static method or function in the current namespace, we write "A\B::foo();
", which indicates that we will call the namespace "A The "foo" method of class "B" defined in \A" automatically loads class "A\A\B" if it is not found.
Now let’s summarize.
Calls to fully qualified functions, classes and constants will be resolved at compile time. For example, new\a\B resolves to class a\B.
All unqualified names and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if namespace A\B\C is imported as C, then calls to C\D\e() are translated to A\B\C\D\e().
All unqualified names and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if namespace a\B\C was imported as C, a call to C\D\e() would be translated to a\B\C\D\e().
Unqualified class names are converted at compile time according to the current import rules (replacing short import names with full names). For example, if the namespace a\B\C was imported as C, the new C() will be converted to the new a\B\C().
Within a namespace (e.g., a\b), function calls to unqualified names are resolved at runtime. For example, a call to function foo() is parsed as follows:
Look for a function named A\B\foo() in the current namespace
Try to find and call function foo() in the global space.
Calls to unqualified names or qualified name classes (non-fully qualified names) within a namespace (such as a\b) are resolved at runtime. The following is the parsing process of calling new c() and new d\e(): Parsing new c():
Find A\B\C in the current namespace kind.
Try to automatically load class A\B\C.
Parsing of new D\E():
Add the current namespace name in front of the class name. into: A\B\D\E, and then look for the class.
Try to autoload class A\B\D\E.
In order to refer to a global class in the global namespace, the fully qualified name new \C() must be used.
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of The editor will teach you the calling sequence of namespaces. For more information, please follow other related articles on the PHP Chinese website!