Broadly speaking, namespaces are a way of encapsulating things. This abstract concept can be found in many places. For example, directories are used in operating systems to group related files, and they act as namespaces for the files in the directory. When this principle is applied to the field of programming, it is the concept of namespace
In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications: one is user-written code and PHP internal classes/functions/ Name conflicts between constants or third-party classes/functions/constants; the other is to create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving Source code readability. PHP namespaces provide a way to group related classes, functions, and constants together. This article will introduce PHP namespace in detail
Although any legal PHP code can be included in a namespace, only the following types of code are affected by the namespace, they are: classes (including abstract classes and traits), interfaces, functions and constants
Namespace is declared through the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code except one: the declare keyword
<?<span style="color: #000000;">php namespace MyProject; </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span>?>
Subnamespace
Much like the relationship between directories and files, PHP namespaces also allow you to specify hierarchical namespace names. Therefore, namespace names can be defined in a hierarchical manner
<?<span style="color: #000000;">php namespace MyProject\Sub\Level; </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span>?>
<?<span style="color: #000000;">php namespace MyProject\Sub\Level; </span><span style="color: #0000ff;">const</span> NUM = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> NUM;<span style="color: #008000;">//</span><span style="color: #008000;">1</span> <span style="color: #0000ff;">echo</span> \MyProject\Sub\Level\NUM;<span style="color: #008000;">//</span><span style="color: #008000;">1</span> ?>
Multiple namespaces can be defined in the same file. There are two syntax forms for defining multiple namespaces in the same file
In actual programming practice, it is highly discouraged to define multiple namespaces in the same file. This method is mainly used to merge multiple PHP scripts into the same file
【1】Simple combination syntax (not recommended)
<?<span style="color: #000000;">php namespace MyProject; </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } namespace AnotherProject; </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span>?>
【2】Braces syntax
<?<span style="color: #000000;">php namespace MyProject { </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } } namespace AnotherProject { </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } } </span>?>
<?<span style="color: #000000;">php namespace MyProject { </span><span style="color: #0000ff;">const</span> NUM = 1<span style="color: #000000;">; } namespace AnotherProject { </span><span style="color: #0000ff;">const</span> NUM = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> NUM;<span style="color: #008000;">//</span><span style="color: #008000;">2</span> <span style="color: #0000ff;">echo</span> \MyProject\NUM;<span style="color: #008000;">//</span><span style="color: #008000;">1</span> <span style="color: #000000;">} </span>?>
Overall
To combine the code in the global non-namespace with the code in the namespace, you can only use the syntax in the form of braces. Global code must be enclosed in curly brackets using a namespace statement without a name
<?<span style="color: #000000;">php namespace MyProject { </span><span style="color: #0000ff;">const</span> CONNECT_OK = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">class</span> Connection { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span> connect() { <span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span><span style="color: #000000;"> } } namespace { </span><span style="color: #008000;">//</span><span style="color: #008000;"> global code</span> <span style="color: #008080;">session_start</span><span style="color: #000000;">(); </span><span style="color: #800080;">$a</span> =<span style="color: #000000;"> MyProject\connect(); </span><span style="color: #0000ff;">echo</span> MyProject\Connection::<span style="color: #000000;">start(); } </span>?>
Unqualified name refers to an identifier that does not contain a namespace separator in the name, such as Foo
Qualified name Qualified name refers to an identifier that contains a namespace delimiter in the name, such as FooBar
Fully qualified name refers to an identifier whose name contains a namespace delimiter and starts with a namespace delimiter, such as FooBar. namespaceFoo is also a fully qualified name
If you want to access an element dynamically (for example, a variable function), you must use a fully qualified name
<?<span style="color: #000000;">php namespace MyProject; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } </span><span style="color: #800080;">$var1</span> = 'test'<span style="color: #000000;">; </span><span style="color: #800080;">$var2</span> = '\MyProject\test'<span style="color: #000000;">; </span><span style="color: #800080;">$var1</span>();<span style="color: #008000;">//</span><span style="color: #008000;">报错</span> <span style="color: #800080;">$var2</span>();/111 ?>
PHP supports two abstract methods of accessing internal elements of the current namespace, __NAMESPACE__ magic constant and namespace keyword
The value of the constant __NAMESPACE__ is a string containing the name of the current namespace. In global code, not included in any namespace, it contains an empty string
<?<span style="color: #000000;">php namespace MyProject; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } </span><span style="color: #800080;">$var</span> = __NAMESPACE__.'\test'<span style="color: #000000;">; </span><span style="color: #800080;">$var</span>();<span style="color: #008000;">//</span><span style="color: #008000;">111</span> ?>
The keyword namespace can be used to explicitly access elements in the current namespace or sub-namespaces. It is equivalent to the self operator in the class
<?<span style="color: #000000;">php namespace MyProject; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } test();</span><span style="color: #008000;">//</span><span style="color: #008000;">111</span> __NAMESPACE__.test();<span style="color: #008000;">//</span><span style="color: #008000;">111</span> namespace\test();<span style="color: #008000;">//</span><span style="color: #008000;">111</span> ?>
If no namespace is defined, all classes and functions are defined in the global space, just like before PHP introduced the namespace concept. Adding a prefix before the name indicates that the name is a name in the global space, even if the name is in another namespace
<?<span style="color: #000000;">php namespace A\B\C; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 这个函数是 A\B\C\fopen </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">function</span> <span style="color: #008080;">fopen</span><span style="color: #000000;">() { </span><span style="color: #008000;">/*</span><span style="color: #008000;"> ... </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$f</span> = \<span style="color: #008080;">fopen</span>(...); <span style="color: #008000;">//</span><span style="color: #008000;"> 调用全局的fopen函数</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$f</span><span style="color: #000000;">; } </span>?>
php允许通过别名引用或导入外部的完全限定名称,是命名空间的一个重要特征。这有点类似于在类unix文件系统中可以创建对其它的文件或目录的符号连接
所有支持命名空间的PHP版本支持三种别名或导入方式:为类名称使用别名、为接口使用别名或为命名空间名称使用别名
在PHP中,别名是通过操作符 use 来实现的
别名
<?<span style="color: #000000;">php namespace hello\world\test; </span><span style="color: #0000ff;">use</span> hello\world\test <span style="color: #0000ff;">as</span> t;<span style="color: #008000;">//</span><span style="color: #008000;">用t来替代hello\world\test</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> demo(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } t\demo();</span><span style="color: #008000;">//</span><span style="color: #008000;">111</span> ?>
as可以省略
<?<span style="color: #000000;">php namespace hello\world\test; </span><span style="color: #0000ff;">use</span> hello\world\test;<span style="color: #008000;">//</span><span style="color: #008000;">用test来替代hello\world\test</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> demo(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } test\demo();</span><span style="color: #008000;">//</span><span style="color: #008000;">111</span> ?>
导入
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> \ArrayObject; </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">new</span> ArrayObject([]);<span style="color: #008000;">//</span><span style="color: #008000;">若不使用"use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象</span> ?>
为了简化,一行中可以包含多个use语句
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">use</span> My\Full\Classname <span style="color: #0000ff;">as</span> Another,<span style="color: #000000;"> My\Full\NSname; </span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span> Another; <span style="color: #008000;">//</span><span style="color: #008000;"> 实例化 My\Full\Classname 对象</span> NSname\subns\func(); <span style="color: #008000;">//</span><span style="color: #008000;"> 调用函数 My\Full\NSname\subns\func</span> ?>