The use base of PHP namespaces (namespace) and example _php instance

Source: Internet
Author: User

The PHP namespace (namespace) is one of the most important new features in PHP 5.3, a concept that has been in C # already, and namespace in PHP is the same as the C # concept.

A. Php namespace mainly solves three kinds of conflicts: constants, functions, classes

Popular understanding: namespace is used to build a directory, namespace the following code in the directory, and the outside of the distinction.

/*
|---------------------------------
|namespace example
|@ Black eyed poet <www.chenwei.ws>
|------------- --------------------
* * *
namespace myself;
function Var_dump ()
{
  echo;
}
Var_dump ();        //Call the Custom function (relative path mode)
\myself\var_dump (); Call the Custom function (absolute path way) 
\var_dump (m);     Call Global (System function)

Note: You cannot have any code before namespace, except declare (); Multiple files can use the same namespace, but content defined under the same namespace cannot conflict. namespace supports child namespaces, such as: namespace \myself\good, which is equivalent to the concept of multilevel catalogs.

Two. Cases where multiple namespaces exist in the same file

1.

/**
 * The same file if the use of multiple namespaces, the general
 wording
/namespace Nihao\shijie;
function Demo ()
{
//...
}
namespace Hello\world;
function test ()
{
  //...
}

\nihao\shijie\demo ();
\hello\world\test ();

2.

/**
 * If multiple namespaces are used in the same file, it is recommended that curly braces be expanded
/namespace nihao\shijie{
function Test_one ()
{
  //  ......
};
}
Namespace hello\world{
function Test_two ()
{
  //...
.. }
\nihao\shijie\test_one ();
\hello\world\test_two ();

Use multiple namespaces within the same file, primarily for projects that incorporate multiple PHP scripts into the same file, and are not actually advocated!

Three. Name resolution rules (several concepts)

1. Unqualified Name: The name does not contain a namespace separator, 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 separator, such as: \nihao\shijie (the concept of 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.