Namespace of PHP Key features

Source: Internet
Author: User
Namespaces are primarily designed to address possible conflicts between classes and functions in your code. This article gives you a description of the key features of PHP namespace, including the definition of namespaces and other knowledge points, need to refer to the friend

Namespaces are primarily designed to solve the problem of possible conflicts between classes and functions in your code, and this feature has long been in other languages, and PHP has been late, and it has spawned the birth of PSR-4, which has spawned the rise of Composer, so it is a very important feature.

Definition of namespaces

A namespace is a container that is primarily designed to recognize the classes and functions under it. Once the namespace is defined, the following code belongs to the namespace, so the definition of the namespace is at the very beginning of the code.

For the same package, the code for the same namespace or sub-namespace is not necessarily defined in a PHP file, and the code under the sub-namespace is designed to complete the work of a particular module, which is a package complete namespace.

If you write code that does not define a namespace, it means that it is a global namespace (\ symbol), so you can refer to a class or function directly (without adding \ symbols).

Three ways to reference a namespace identifier

(1) fully-qualified name

Similar to the absolute path on the operating system, and is the complete path, so there is no misunderstanding when understanding.

For example, in new \a\b\c, C is parsed into Class C under the a\b namespace.

(2) Qualified name

Similar to the relative path on the operating system, it contains partial names and is referenced to the current namespace.

For example, if b\c () is down in namespace A, the final reference namespace is a\b\c ().

(3) Unqualified name

is similar to qualified name, but does not include a sub-namespace.

For example, if C () is a\b in the namespace, the final reference namespace is a\b\c ().

Use an example to illustrate three ways of referencing:

namespace \example;require_once "fnction.php"; class ClassA {}function function () {}//fully qualified name \example\function (); \ Example\b\function (); Qualified name b\function (); Point to \example\b\function ();//unqualified name $test = new ClassA (); resolves to \example\classafunction (); Point to \example\function

Attention:

inside a namespace, if the definition of functions and constants is not found in current scope, PHP will not error. Instead, go to the global namespace and look for it.

inside a namespace, if the definition of the class is not found in current scope, then PHP will directly error, will not go to the global domain to find the corresponding class, so if you need to reference a internal or user-defined class, you must use the fully qualified name.

To give a simple example, first write a piece of code (defined under a namespace), named function.php:

namespace Foo\bar\subnamespace;const foo = 1;function foo () {  return ' foo\r\n ';} class Foo {  static function Staticmethod ()  {    return __method__. "\ r \ n";  }  function foofunction ()  {    return __method__. "\ r \ n";  }}

Write another piece of code test.php, which is also the code under the namespace:

namespace Secondsp;include ' function.php '; class foo{  function foofunction ()  {    return __method__. "\ r \ n";  }} function Is_file ($file) {  return true;} Unqualified Name: Instantiate Secondsp\foo class object $obj = new Foo; echo $obj->foofunction ();//Instantiate Foo\bar\subnamespace\foo class object $obj = new Foo\bar\subnamespace\foo; Echo $obj Foofunction ();//code will error, within the namespace, if the current namespace can not be found in the class, will be error//$obj = new Arrayobject (Array (1)); $obj = new \arrayobject (Array (1)); Inside the namespace, if the function or constant under the current namespace cannot be found, the native Functionecho strlen ("nihao") is searched; Refers to the function var_dump under the current namespace (is_file (' Nihao ')); true//refers to the global function Var_dump (\is_file (' Nihao ')); False

Import, alias

If you want to use a very long and large number of namespaces, it is particularly cumbersome to use, so you can import namespaces, classes, constants, functions, and so on using the USE keyword, and then use them to refer directly to the full name. The alias keyword can be used to rename imported classes and functions.

For example, using the USE keyword, the code is under the global namespace:

Include ' function.php '; use Foo\bar\subnamespace\foo; $obj = new Foo;echo $obj->foofunction (), use foo\bar\ Subnamespace\foo as aliasfunname; $obj = new Aliasfunname;echo $obj->foofunction (); Use foo\bar\subnamespace; $obj = new Subnamespace\foo; echo $obj->foofunction (); use Foo\bar\subnamespace as aliasname; $obj = new Aliasname\foo; e Cho $obj->foofunction ();//Because the calling code is not within the namespace, there is no need for a global class to be introduced using $obj = new Arrayobject (Array (1)); Import a function with use function Foo\bar\subnamespace\foo  ; echo Foo (), use function Foo\bar\subnamespace\foo as Func;echo func ( ); use const Foo\bar\subnamespace\foo;//echo Foo;

Summarize:

• Unlike Python, a namespace in PHP is a semantic concept that has no relation to the location or layout of the specific code, in other words, the code that uses the namespace needs to introduce itself to the library file (all files), as to how the library file is organized, and in Python, if a module or package has a __init__.py file, the Python parser automatically introduces the files for the package or for all modules.

The concept of scope in PHP is very weak, and the global domain and local domain are very clear, such as the inability to reference variables in the global space in a function or class. In the namespace, however, the code that defines the namespace will use global constants and functions if the constants and functions under the corresponding namespace are not found, and if the class under the corresponding name namespace (including the custom Class) is not found, the code directly complains.

• Using a namespace with the Use keyword does not need to be imported by means of a fully qualified name (\ symbol), because PHP has assumed that the imported is a fully qualified namespace.

• You can import constants, functions, classes, interfaces, and other namespaces by using the USE keyword.

• Namespaces are a language feature, and for more efficient use, there should be a usage specification and an automatic loading mechanism, which is the PSR-4 specification.

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.