[Namespace] PHP namespace Usage basics, namespace namespaces
--------------------------------------------------------------------------------------------------
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
|---------------------------------
*/namespace myself; function Var_dump () { echo ;} Var_dump (100); // call Custom Function (relative path mode)\myself\var_dump(100); // \var_dump(+); // 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)
------------------------------------------------------------------------------------------------
PHP Use PHP namespace What's the matter?
1. Namespace Zend\http\phpenvironment;
This code defines a namespace that you can understand to define a domain name called zend\http\phpenvironment.
After the definition, the following class, interface, const, etc. are stated in the "domain" of the declaration. When referencing a containing file that declares a namespace, and wants to invoke something inside it, it must:
Adjust the current script also to this domain name, otherwise, you will have to use the full name of NAMESAPCE.
For example, inc.php file:
namespace Zend\http\phpenvironment;
Class Bar {}//defines a
When other files are called:
The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();
The second way to access Foo
namespace Foo; Adjust the current script to foo this NS domain, and namespace declaration must be in the first sentence
Require ' inc.php ';
$foo = new Bar ();
2. The Use keyword is used to alias the NS:
For example, the above
The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();
After using uses, the following wording is used:
Use \zend\http\phpenvironment as PE; Defining aliases
$foo = new \pe\bar (); Replace the original with a short alias.
If you omit the following as ..., then you can use the text in the last section instead, for example, above:
Use \zend\http\phpenvironment; Defining aliases
$foo = new \phpenvironment\bar (); Replace the original with a short alias.
================================================
Relevant content in the official PHP manual:
In PHP, the namespace namespace is used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:
1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.
The PHP namespace provides a way to group related classes, functions, and constants together.
The PHP namespace supports two ways of using aliases or imports: using aliases for class names, or aliases for namespace names, which are implemented using operator use. ... Remaining full text >>
Php namespace namespace error?
There's no grammatical problem.
What is your PHP version? PHP began supporting namespaces after version 5.3.0. Your PHP version may be lower.
http://www.bkjia.com/PHPjc/864817.html www.bkjia.com true http://www.bkjia.com/PHPjc/864817.html techarticle [The Namespace]php namespace is used based on the namespace namespace--------------------------------------------------------------------- -----------------------------A. php name empty ...