Home  >  Article  >  Backend Development  >  Detailed explanation of php namespace usage

Detailed explanation of php namespace usage

PHPz
PHPzOriginal
2020-09-25 14:24:512596browse

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;".

Detailed explanation of php namespace usage

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;

You can also define different namespace codes in the same file, such as:

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.

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:

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.

The following code will cause a syntax error:


” 会致命错误 - 命名空间必须是程序脚本的第一条语句
?>

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:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn