PHP namespaces were added in PHP 5.3, and if you’ve learned C# or Java, namespaces are nothing new. But in PHP, it’s important.

PHP namespaces solve two types of problems:

Name conflicts between user-written code and internal PHP classes/functions/constants or third-party classes/functions/constants. Create an alias (or short) name for a long identifier name (usually defined to alleviate type 1 problems) to improve the readability of the source code. Defining namespaces By default, all constant, class, and function names are placed in the global space, just as PHP did before namespaces were supported.

Namespaces are declared by the namespace keyword. If a file contains a namespace, it must declare the namespace before all other code. The syntax format is as follows;

It is not recommended to define multiple namespaces in a single file using this syntax. It is recommended to use the syntax in curly braces below.

To combine code in a global non-namespace with code in a namespace, use only syntax in curly braces. Global code must be enclosed in braces with an unnamed namespace statement, for example:

The only code that is legal before declaring a namespace is the DECLARE statement that defines how the source file is encoded. All non-PHP code including whitespace cannot precede namespace declarations.

The following code has a syntax error:

The above example creates the constant MyProject\Sub\Level\CONNECT_OK, the class MyProject\Sub\Level\Connection, and the function MyProject\Sub\Level\Connect.

Namespaces Using class names in PHP namespaces can be referenced in one of three ways:

An unqualified name, or a class name without a prefix, such as $a=new foo(); Or foo: : staticmethod (); . If the currentnamespace is currentnamespace, foo will be resolved to currentnamespace\foo. If the code that uses Foo is global, code that is not contained in any namespace, then Foo is resolved to Foo. Warning: If a function or constant in a namespace is not defined, the unqualified function or constant name is resolved to a global function or constant name.

A qualified name, or a name with a prefix, such as $a = new subnamespace\foo(); Or subnamespace \ foo: : staticmethod (); . If the currentnamespace is currentnamespace, foo will be resolved to currentnamespace\subnamespace\foo. If the code that uses foo is global and not contained in any namespace, foo is resolved to subnamespace\foo.

Fully qualified names, or names that include global prefix operators, for example, $a = new \ currentNamespace \foo(); Or \ currentnamespace \ foo: : staticmethod (); . In this case, foo is always resolved to literal name currentNamespace \foo in the code.

Here is an example using all three:

File1.php file code

File2.php file code

Note that you can use fully qualified names to access any global class, function, or constant, such as \strlen() or \Exception or \INI_ALL.

Accessing global classes, functions, and constants within a namespace:

Namespaces and Dynamic Language Characteristics The implementation of PHP namespaces is influenced by the dynamic characteristics of the language itself. Therefore, if you want to convert the following code into a namespace, access elements dynamically.

Example1.php file code:

Fully qualified names (including class names with namespace prefixes) must be used. Note that because there is no difference between a qualified name and a fully qualified name in a dynamic class name, function name, or constant name, the leading backslash is unnecessary.

Dynamically access elements of a namespace

Namespace keyword and __NAMESPACE__ constant PHP supports two abstract methods for accessing elements inside the current namespace, the namespace magic constant and the namespace keyword.

The value of the constant __NAMESPACE__ is a string containing the name of the current namespace. Global, not included in any namespace code, which contains an empty string.

NAMESPACE example, code in a NAMESPACE

NAMESPACE example, global code

A constant NAMESPACE is useful when creating names dynamically, for example:

Create names dynamically using __NAMESPACE__

The namespace keyword can be used to explicitly access elements in the current namespace or subnamespace. It is equivalent to the self operator in the class.

Namespace operator, code in a namespace

Namespace operator, global code

Using namespaces: Alias/import PHP namespaces support two ways of using aliases or imports: using aliases for class names, or using aliases for namespace names.

In PHP, aliases are implemented through the use operator. Here is an example using all three possible import methods:

Import/use aliases using the use operator

2. Multiple use statements in a single line

Import operations are performed at compile time, but dynamic class, function, or constant names are not.

Import and dynamic names

In addition, the import operation affects only unqualified and qualified names. Fully qualified names are not affected by imports because they are deterministic.

4. Import and fully qualify names

Use namespaces: Backup global functions/constants In a namespace, when PHP encounters an unqualified class, function, or constant name, it uses a different priority policy to resolve the name. Class names are always resolved to names in the current namespace. So fully qualified names must be used when accessing class names inside the system or not contained in the namespace, for example:

1. Access global classes in the namespace

For functions and constants, PHP falls back to using functions or constants in global space if the function or constant does not exist in the current namespace.

2. Global function/constant backup in the namespace

Global space If no namespace is defined, all classes and functions are defined in the global space, just as before PHP introduced the concept of namespaces. The prefix \ before a name indicates that the name is in the global space, even if the name is in another namespace.

Use global space specification

Since the creation of namespaces, one of the most error-prone aspects of using a class is what the search path for that class looks like.

Name resolution follows the following rules:

Calls to functions, classes, and constants with fully qualified names are resolved at compile time. For example, new \A\B resolves to class A\B. All unqualified and qualified (not fully qualified) names are converted at compile time based on the current import rules. For example, if the namespace A\B\C is imported as C, the call to C\D\e() will be converted to A\B\C\D\e(). Inside the namespace, all qualified names that are not converted according to import rules are preceded by the current namespace name. For example, if C\D\e() is called inside the namespace A\B, C\D\e() will be converted to A\B\C\D\e(). Unqualified class names are converted at compile time according to the current import rules (full names instead of short import names). For example, if namespace A\B\C is imported as C, then new C() is converted to new A\B\C(). Inside A namespace (for example, A\B), function calls to unqualified names are resolved at run time. For example, A call to function foo() is resolved like this: Looking for A function named A\B\foo() in the current namespace tries to find and call function foo() in the global space. Calls to unqualified names or qualified name classes (not fully qualified names) within A namespace (such as A\B) are resolved at run time. Here’s how to parse new C() and new D\E() : Parse new C() : find A\B\C class in the current namespace. Try auto-loading class A\B\C. Parsing of new D\E() : A\B\D\E becomes: A\B\D\E by prefacing the class name with the current namespace name, and then looking for the class. Try auto-loading class A\B\D\E. To reference a global class in a global namespace, the fully qualified name new \C() must be used. PHP magic constants PHP object-oriented 1 notes to write notes Alex Gump

182***[email protected]

Refer to the address

44 Unqualified names can be likened to file names (such as comment.php),. Qualified name analogy for the relative path names (for example. / article/comment. PHP), the fully qualified name for the absolute path name (for example/blog/article/comment. PHP), it may be easier to understand.

One more example:

For more information, see: How to use Namespace in PHP

Source code free download address: github.crmeb.net/u/defu