[Namespace] Basics of using PHP namespaces

Source: Internet
Author: User

--------------------------------------------------------------------------------------------------

One. PHP namespaces primarily address three conflicting issues: constants, functions, classes

Popular understanding: namespace is equivalent to building a directory, namespace the following code in the directory, and outside the distinction.

/*|---------------------------------|namespace Example |@ black-eyed poet <www.chenwei.ws>|------------------------------- --*/namespace myself;function Var_dump(){    Echo100;}Var_dump(100);//calling a custom function (relative path mode)\myself\Var_dump(100);//Calling custom functions (absolute path mode)\Var_dump(100);//Call Global (System function)

Note: namespace cannot have any code before, except declare (); Multiple files can use the same namespace, but the content defined under the same namespace cannot be conflicting. Namespace supports sub-namespaces, such as: namespace \myself\good, which is equivalent to the concept of multi-level catalogs.

Two. Cases where multiple namespaces exist in the same file

1.

/**
* chenwei.ws
*/
namespace Nihao\shijie;namespace Hello\world; function Test () { //... .. }// consecutive naming, the following code will use the second namespace, and all successive naming is meaningless.

2.

/**/namespace nihao\shijie{  function  test_one ()
{ //...... };} namespace hello\world{ function test_two ()
{ //........ }}\nihao\shijie\test_one (); \hello\world\test_two ();

The use of multiple namespaces within the same file, mainly for projects to merge multiple PHP scripts in the same file, is not advocated in practice!

Three. Name resolution rules (several concepts)

1. Unqualified Name: The name does not contain a namespace delimiter, such as: Myself

2. Qualified Name: The name contains a namespace separator, such as: Nihao\shijie

3. Fully qualified name: The name contains a delimiter and begins with a namespace delimiter, such as: \nihao\shijie (that is, the concept of an absolute path)

------------------------------------------------------------------------------------------------

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.