Frontend PHP namespace and PHP namespace

Source: Internet
Author: User

Frontend PHP namespace and PHP namespace
* Directory [1] Definition [2] multi-namespace [3] name resolution [4] Access internal element [5] global space [6] alias and import the previous words

In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. For example, a directory in the operating system is used to group related files. For files in the directory, it plays the role of namespace. This principle is applied to the field of programming as a namespace concept.

In PHP, The namespace is used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications: one is the name conflict between the code written by the user and the class/function/constant in PHP or a third-party class/function/constant; another type is to create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem) to improve the readability of the source code. The PHP namespace provides a way to combine related classes, functions, and constants. This article will introduce the PHP namespace in detail

 

Definition

Although any valid PHP code can be included in the namespace, only the following types of code are affected by the namespace: Class (including abstract classes and traits), interfaces, functions, and constants

The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other Code, except for the declare keyword.

<?phpnamespace MyProject;const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }?>

Sub-namespace

It has a similar relationship with directories and files. The PHP namespace also allows you to specify a hierarchical namespace name. Therefore, namespace names can be defined in different layers.

<?phpnamespace MyProject\Sub\Level;const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }?>
<?php    namespace MyProject\Sub\Level;    const NUM = 1;    echo NUM;//1    echo \MyProject\Sub\Level\NUM;//1?>

 

Multiple namespaces

Multiple namespaces can be defined in the same file. Defining multiple namespaces in the same file has two syntax forms.

In actual programming practices, it is not recommended to define multiple namespaces in the same file. This method is mainly used to merge multiple PHP scripts into the same file.

[1] Simple combination syntax (not recommended)

<?phpnamespace MyProject;const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }namespace AnotherProject;const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }?>

[2] braces syntax

<?phpnamespace MyProject {const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }}namespace AnotherProject {const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }}?>
<?phpnamespace MyProject {    const NUM = 1;}namespace AnotherProject {    const NUM = 2;    echo NUM;//2    echo \MyProject\NUM;//1}?>

Global

The code in a global non-namespace is combined with the code in the namespace. You can only use the syntax in braces. The global code must be enclosed in braces using a namespace statement without a name.

<?phpnamespace MyProject {const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }}namespace { // global codesession_start();$a = MyProject\connect();echo MyProject\Connection::start();}?>

 

Name resolution

Unqualified name is an identifier that does not contain a namespace separator. For example, Foo

The Qualified name is the identifier of the namespace Separator in the name, such as Foo \ Bar.

The Fully qualified name is an identifier that contains a namespace separator and starts with a namespace separator, for example, \ Foo \ Bar. Namespace \ Foo is also a fully qualified name

To dynamically access elements (such as variable functions), you must use a fully qualified name.

<? Phpnamespace MyProject; function test () {echo '000000';} $ var1 = 'test'; $ var2 = '\ MyProject \ test'; $ var1 (); // error $ var2 ();/111?>

 

Access internal elements

PHP supports two abstract methods to access the internal elements of the current NAMESPACE, __namespace _ magic constant and namespace keyword

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

<?phpnamespace MyProject;    function test(){        echo '111';    }    $var = __NAMESPACE__.'\test';    $var();//111?>

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.

<?phpnamespace MyProject;    function test(){        echo '111';    }    test();//111    __NAMESPACE__.test();//111    namespace\test();//111?>

 

Global Space

If no namespace is defined, all classes and functions are defined in the global space, just as before the introduction of the namespace concept in PHP. Add the prefix \ before the name to indicate that the name is a name in the global space, even if the name is in another namespace

<? Phpnamespace A \ B \ C;/* this function is A \ B \ C \ fopen */function fopen (){/*... */$ f = \ fopen (...); // call the global fopen function return $ f;}?>

 

Alias and import

Php allows alias reference or external fully qualified names to be imported, which is an important feature of a namespace. This is similar to creating a symbolic connection to other files or directories in a unix-like file system.

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

In PHP, aliases are represented by operators.UseTo achieve

Alias

<? Phpnamespace hello \ world \ test; use hello \ world \ test as t; // use t to replace hello \ world \ testfunction demo () {echo '000000 ';} t \ demo (); // 111?>

As can be omitted

<? Phpnamespace hello \ world \ test; use hello \ world \ test; // use test to replace hello \ world \ testfunction demo () {echo '000000 ';} test \ demo (); // 111?>

Import

<? Phpuse \ ArrayObject; $ a = new ArrayObject ([]); // If "use \ ArrayObject" is not used, a foo \ ArrayObject object is instantiated.?>

To simplify the process, a row can contain multiple use statements.

<? Phpuse My \ Full \ Classname as Another, My \ Full \ NSname; $ obj = new Another; // instantiate the My \ Full \ Classname object NSname \ subns \ func (); // call the function My \ Full \ NSname \ subns \ func?>

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.