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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
