Note 008PHP namespace-Part 1

Source: Internet
Author: User
Here, we can make a simple analogy between the PHP namespace and the file system. There are three methods to access a file in a file system:

Use namespace: Basic

PHP determines which namespace to use to send elements

Here, we can make a simple analogy between the PHP namespace and the file system. There are three methods to access a file in a file system:

The relative file name is similar to foo.txt. It will be parsed to currentdirectory/foo.txt, where currentdirectory indicates the current directory. Therefore, if the current directory is/home/foo, the file name is resolved to/home/foo/foo.txt.

The relative path name format is subdirectory/foo.txt. It will be parsed as currentdirectory/subdirectory/foo.txt.

The absolute path name format is/main/foo.txt. It will be parsed to/main/foo.txt.

PHP namespaces use the same principle. For example, the class name can be normally referenced in the following three methods:

A non-qualified name or a class name that does not contain a prefix, for example, $ a = new foo (); or foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as foo. Warning if the function or constant in the namespace is not defined, the undefined function name or constant name will be resolved to the global function name or constant name. For more information, see use namespace: backup global function name/constant name.

A qualified name, or a name containing a prefix, such as $ a = new subnamespace \ foo (); or subnamespace \ foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ subnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as subnamespace \ foo.

A fully qualified name or a name that contains the global prefix operator, for example, $ a = new \ currentnamespace \ foo (); or \ currentnamespace \ foo: staticmethod ();. In this case, foo is always parsed as the text name (literal name) currentnamespace \ foo in the code.

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

Key points of namespaces in dynamic classes

Example1.php

 

To convert the following code to a namespace, you must use a fully qualified name (including the class name with the namespace prefix ). Note that in the dynamic class name, function name, or constant name, the qualified name and fully qualified name are no different, so the leading backslash is unnecessary.

 

Namespace keyword and NAMESPACE constant

PHP supports two abstract methods to access the internal elements of the current NAMESPACE, the namespace magic constant and the NAMESPACE keyword. Note: the magic constant has two underscores.

NAMESPACE

The constant NAMESPACE value is a string containing the name of the current NAMESPACE. Globally, code that is not included in any namespace contains an empty string.

 

The global code outputs an empty string when no namespace is available.

Constant NAMESPACE is useful for dynamically creating names, for example:

 

Namespace

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

 

Use the alias/import in the namespace

Allowing alias reference or external fully qualified names is an important feature of a namespace. This is similar to creating symbolic connections to other files or directories in unix-like file systems.

All PHP versions that support namespaces support three alias or import methods: use an alias for the class name, use an alias for the interface, or use an alias for the namespace name. PHP 5.6 allows you to import functions or constants or set aliases for them.

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

 

Note: For the name in the namespace (the fully qualified name that contains the namespace separator, such as Foo \ Bar, and the global name that does not contain the namespace separator, such as FooBar, the leading backslash is unnecessary and not recommended, because the imported name must be fully qualified and will not be parsed relative to the current namespace.

To simplify the operation, PHP also supports multiple use statements in one row.

 

The import operation is compiled and executed, but the dynamic class name, function name, or constant name is not.

 

In addition, the import operation only affects non-qualified names and qualified names. The fully qualified name is determined, so it is not affected by the import.

 

Resolution priority policy

In a namespace, when PHP encounters an unqualified class, function, or constant name, it uses different priority policies to parse the name.

Class resolution priority

The class name is always resolved to the name in the current namespace. Therefore, you must use a fully qualified name to access the class name in the system or that is not included in the namespace. for example:

 

Resolution priority of functions and constants

For functions and constants, if this function or constant does not exist in the current namespace, PHP will return and use the function or constant in the global space.

 

The above is the content in the namespace-Part 1 of note 008 PHP. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.