php namespace refers to the namespace. How to use it: first create a PHP sample file; then define the code in the "MyProject" namespace through "namespace MyProject;".
PHP namespace (namespace)
PHP namespace (namespace) was added in PHP 5.3. If you have learned C# and Java, namespaces are nothing new. However, it still has a very important significance in PHP.
PHP namespace can solve the following two types of problems:
Name conflicts between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.
Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) to improve the readability of the source code.
Define namespace
By default, all constant, class and function names are placed in the global space, just like before PHP supported namespaces.
The namespace is declared through the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. The syntax format is as follows;
<?php // 定义代码在 'MyProject' 命名空间中 namespace MyProject; // ... 代码 ...
You can also define different namespace codes in the same file, such as:
<?php namespace MyProject; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } namespace AnotherProject; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } ?>
It is not recommended to use this syntax to define multiple namespaces in a single file. . It is recommended to use the following curly bracket form of syntax.
<?php namespace MyProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } namespace AnotherProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } ?>
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 braces with an unnamed namespace statement, for example:
<?php namespace MyProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } namespace { // 全局代码 session_start(); $a = MyProject\connect(); echo MyProject\Connection::start(); } ?>
The only legal code before declaring a namespace is the declare statement that defines how the source file is encoded. All non-PHP code, including whitespace, must not appear before a namespace declaration.
<?php declare(encoding='UTF-8'); //定义多个命名空间和不包含在命名空间中的代码 namespace MyProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } namespace { // 全局代码 session_start(); $a = MyProject\connect(); echo MyProject\Connection::start(); } ?>
The following code will cause a syntax error:
<html> <?php namespace MyProject; // 命名空间前出现了“<html>” 会致命错误 - 命名空间必须是程序脚本的第一条语句 ?>
Subnamespace
Much like the relationship between directories and files, PHP namespaces also allow the specification of hierarchical namespaces The name. Therefore, namespace names can be defined in a hierarchical manner:
<?php namespace MyProject\Sub\Level; //声明分层次的单个命名空间 const CONNECT_OK = 1; class Connection { /* ... */ } function Connect() { /* ... */ } ?>
The above example creates the constant MyProject\Sub\Level\CONNECT_OK, the class MyProject\Sub\Level\Connection and the function MyProject\Sub\Level \Connect.
Namespace usage
Class names in PHP namespaces can be referenced in three ways:
Unqualified names, or class names that do not include a prefix, such as $a =new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespace\foo. If the code using foo is global and does not contain code in any namespace, foo will be resolved as foo. Warning: If a function or constant in a namespace is undefined, the unqualified function or constant name is resolved to a global function or constant name.
Qualified name, or name containing a prefix, such as $a = new subnamespace\foo(); or subnamespace\foo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespace\subnamespace\foo. If the code using foo is global, code not contained in any namespace, foo will be resolved to subnamespace\foo.
Fully qualified name, or a name that includes a global prefix operator, for example, $a = new \currentnamespace\foo(); or \currentnamespace\foo::staticmethod();. In this case, foo is always resolved to the literal name currentnamespace\foo in the code.
The above is the detailed content of Detailed explanation of php namespace usage. For more information, please follow other related articles on the PHP Chinese website!