Home > Article > Backend Development > Namespace definition and use in PHP (detailed examples)
In the previous article, I brought you "How to get PHP class inheritance?" (Summary sharing) 》, which introduces the relevant knowledge about inheritance in PHP classes in detail. In this article, we will continue to look at the relevant knowledge about PHP namespaces. I hope it will be helpful to everyone!
Namespace is actually an abstract concept. For example, in our daily life, directories in the operating system are used to group related files. For files in the directory Say, it plays the role of a namespace.
SoWhat is a namespace? In fact, namespace can be understood as a method of encapsulating things. Classes, functions, and constants in PHP cannot have the same name. In order to prevent them from having the same name and solve the problem of the same name among the three, namespaces need to be used.
In PHP, namespaces are mainly used to resolve naming conflicts between user-written code and PHP internal or third-party classes, functions, and constants. When there are too many files, there is always the possibility of duplicate naming. ;Also create a very short name for a long identifier name, which will improve the readability of the code.
So how is a namespace declared, that is, defined and used? Next, let’s take a look at how to define the namespace!
Define namespace
Any correct code in PHP can be included in the namespace, but only classes, functions, constants, etc. Only the code of the type will be affected by the namespace.
We use the namespace
keyword to complete the definition of the namespace. Its syntax format is as follows:
namespace 命名空间名;
The example is as follows:
<?php // 定义代码在 'named' 命名空间中 namespace named; //在这里可以不使用大括号 // ... 代码 ... ?>
Define two namespaces:
<?php namespace MyProject { //这里建议大家使用大括号,这里定义了一个名为MyProject的命名空间 const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } namespace Another { //这里定义了一个名为Another的命名空间 const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } ?>
Define sub-namespaces
The relationship between namespaces in PHP is very similar to directories and files, allowing you to specify hierarchical namespace names. Therefore, the name of the namespace can be defined in a hierarchical manner, and its syntax is as follows:
namespace App\Model; namespace App\Controller\Home;
The example is as follows:
<?php namespace MyProject\Sub\Level; //声明分层次的单个命名空间 const CONNECT_OK = 1; class Connection { /* ... */ } function Connect() { /* ... */ } ?>
In the above example, create The constant MyProject\Sub\Level\CONNECT_OK, the class MyProject\Sub\Level\Connection and the function MyProject\Sub\Level\Connect
are defined in the same file. Namespace
We have two syntax formats to define multiple namespaces in one file. The example is as follows:
The first is a simple syntax combination
<?php namespace named; const CONNECT_OK = 1; class className { /* ... */ } namespace names; const CONNECT_OK = 1; class className { /* ... */ } ?>
and then the braces {}
<?php namespace named{ const CONNECT_OK = 1; class className { /* ... */ } } namespace names{ const CONNECT_OK = 1; class className { /* ... */ } } ?>
Through the above introduction, we already know how to define a namespace, just define the naming The space is not enough, it is more important that we use it in PHP, then let's take a look at how to use the namespace.
Using namespaces
Before understanding how to use namespaces, we should understand how to use namespaces in PHP To know which element in the namespace to use, then we need to know the principle of namespace element access in PHP. First of all, we don't know much about PHP, but we can summarize the three ways to access files in the file system:
relative file name, relative path name and absolute path name.
The elements of the PHP namespace only use the same principle. For example, the class name under the namespace can be introduced in three ways:
Unqualified name, or a class name without a prefix, such as $a = new test()
or test
, if the current namespace is currentnamespace
, then test will Resolved as currentnamespace\test
. If the code of test is global and does not contain any code in the namespace, then test will be parsed as test.
Qualify the name , or include a prefix name, such as $a = new subnamespace\test()
, if the current namespace is currentnamespace
, then test will be parsed as currentnamespace\subnamespace\test
. If the code of test is global and does not contain any code in the namespace, then test will be parsed as subnamespace\foo
.
Fully qualified name, or a name that includes a global prefix operator, such as $a = new \currentnamespace\test()
, In this case, test is always resolved to the literal name currentnamespace\test
in the code.
The following is an example of using these three methods. We need two PHP source files, namely demo.php
and index.php
, the sample code is as follows:
<?php namespace Test\Bar\Demo; const FOO = 1; function foo() {} class foo { public function demo() { echo '命名空间为:Test\Bar\Demo'; } } ?>
<?php namespace Foo\Bar; include 'Demo.php'; const FOO = 2; function foo() { echo 'Foo\Bar 命名空间下的 foo 函数<br>'; } class foo { static function demo(){ echo '命名空间为:Foo\Bar<br>'; } } /* 非限定名称 */ foo(); // 解析为 Foo\Bar\foo resolves to function Foo\Bar\foo foo::demo(); // 解析为类 Foo\Bar\foo 的静态方法 staticmethod。 echo FOO.'<br>'; // 解析为常量 Foo\Bar\FOO /* 限定名称 */ Demo\foo(); // 解析为函数 Foo\Bar\Demo\foo Demo\foo::demo(); // 解析为类 Foo\Bar\Demo\foo, // 以及类的方法 demo echo Demo\FOO.'<br>'; // 解析为常量 Foo\Bar\Demo\FOO /* 完全限定名称 */ \Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo \Foo\Bar\foo::demo(); // 解析为类 Foo\Bar\foo, 以及类的方法 demo echo \Foo\Bar\FOO.'<br>'; // 解析为常量 Foo\Bar\FOO ?>
In the above example, we need to note that to access any global class, function or constant, you can use a fully qualified name, such as \strlen()
or \Exception
etc.
别名、导入
PHP 允许通过别名引用或导入的方式来使用外部的命名空间,这是命名空间的一个重要特征。
在PHP中,通过use
关键字和as
配合可以实现命名空间的导入和设置别名。它的语法格式如下:
use 命名空间 as 别名;
示例如下:
<?php namespace foo; use My\Full\Classname as Another; // 下面的例子与 use My\Full\NSname as NSname 相同 use My\Full\NSname; // 导入一个全局类 use ArrayObject; // 导入一个函数 use function My\Full\functionName; // 导入一个函数并定义别名 use function My\Full\functionName as func; // 导入一个常量 use const My\Full\CONSTANT; $obj = new namespace\Another; // 实例化 foo\Another 对象 $obj = new Another; // 实例化 My\Full\Classname 对象 NSname\subns\func(); // 调用 My\Full\NSname\subns\func 函数 $a = new ArrayObject(array(1)); // 实例化 ArrayObject 对象 // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象 func(); // 调用 My\Full\functionName 函数 echo CONSTANT; // 打印 My\Full\CONSTANT 常量 ?>
其中需要注意的是,导入操作只影响非限定名称和限定名称。完全限定名称由于是确定的,故不受导入的影响。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of Namespace definition and use in PHP (detailed examples). For more information, please follow other related articles on the PHP Chinese website!