put forward
Before namespace suggested that different components can easily run into conflict named, for example Request
, Response
other common name. After PHP 5.3 namespaces proposed to resolve naming conflicts between components, the main reference design file systems:
- The same directory not have the same file name - under one namespace has the same category are not allowed;
- Different directories can have the same name as the file - different namespaces can have the same class;
definition
Use namespace
keyword to define a namespace. Among them, the top-level namespace is usually manufacturer name, different developers vendors namespace is unique. Namespace does not need to correspond with the file directory, but it is best to comply with PSR-4 specification.
. 1 ? < PHP 2 . 3 namespace Symfony \ the Component \ HttpFoundation; . 4 . 5 class the Request { . 6 . 7 } . 8 namespace must be declared before all the code, the only exception is declare keyword. . 9 10 ? < PHP . 11 12 is DECLARE (= strict_types. 1 ); 13 is 14 namespace the App;
The namespace may contain any PHP code, but only classes (including abstract and Trait), interfaces, functions, constants, and these four types of effect.
1 <?php 2 namespace MyProject; 3 4 const CONNECT_OK = 1; 5 class FOO {} 6 interface Foo{} 7 function foo() {}
use
Use use
keywords to introduce namespaces
1 <?php 2 3 namespace App; 4 5 use Symfony\Component\HttpFoundation\Request; 6 use Foo\Bar; 7 8 class Test { 9 public function run() 10 { 11 $bar = new Bar(); 12 } 13 }
Define and use the recommended follow PSR-2 specification
namespace
Then there must be a blank line;- All
use
statements must be locatednamespace
after the declaration; - Each
use
statement must have only oneuse
keyword.use
There must be a blank line after the block.
When the use
introduction of the same name as Appearing, may be used as
to define aliases
1 <?php 2 3 namespace App; 4 5 use Foo\Bar as BaseBar; 6 7 class Bar extends BaseBar { 8 9 }
Qualifier
In addition to use use
, but also can be used as \
qualifier to be resolved, the rule is simple: if it contains \
prefix represents global namespace from the start parsing, or represents a namespace from the beginning of the current resolution.
. 1 ? < PHP 2 . 3 namespace the App; . 4 . 5 \ Foo \ Bar \ foo (); // resolved into \ Foo \ Bar \ foo (); . 6 Foo \ Bar \ foo (); // resolves App \ Foo \ bar \ foo (); . 7 this rule also applies to functions, constants, and the like . 8 . 9 $ A = \ strlen ( 'Hi'); // call the global function strlen 10 $ B = \ INI_ALL; // access global constants INI_ALL . 11 $ C = new new \ Exception ( 'error'); // instantiate global class Exception
There are two places that require special attention:
For functions and constants, if the current namespace does not exist, it will automatically go to find the global namespace, it can be omitted \
prefix. For the class, if the current namespace not resolve, not to look for the global space, therefore, can not be omitted \
. 1 $ A = strlen ( 'Hi' ); 2 $ B = INI_ALL; . 3 $ C = new new Exception ( 'error'); // Error . 4 $ C = new new \ Exception ( 'error'); // correct
When the dynamic invocation namespace, the namespace will always be treated as a global name space, so you can omit the prefix \
. 1 $ Class1 = 'Foo \ Bar' ; 2 $ object1 = new new $ Class1 ; // always be resolved into \ Foo \ Bar
Access namespace inside
PHP supports two abstract elements within the current namespace access method, __NAMESPACE__
Magic constants and namespace
keywords.
__NAMESPACE__
The value of the constant is a string containing the current namespace name, if it is in the global namespace, an empty string is returned.
1 <?php 2 namespace MyProject; 3 4 function get($classname) 5 { 6 $a = __NAMESPACE__ . '\\' . $classname; 7 return new $a; 8 }
Keywords namespace
can be used to explicitly access the current namespace name or sub-element space. Which is equivalent to the class of self
operator
1 namespace App; 2 3 use blah\blah as mine; 4 5 blah\mine(); // App\blah\mine() 6 namespace\blah\mine(); // App\blah\mine() 7 8 namespace\func(); // App\func() 9 namespace\sub\func(); // App\sub\func() 10 namespace\cname::method(); // App\cname::method() 11 $a = new namespace\sub\cname(); // App\sub\cname 12 $b = namespace\CONSTANT; // App\CONSTANT
Escape \
symbol
In addition, it is recommended for all \
escape, to avoid unpredictable consequences
. 1 $ class = "Dangerous \ name"; // \ newline is parsed into n- 2 $ obj = new new $ class ; . 3 . 4 $ class = 'Dangerous \ name'; // correct, but not recommended . 5 $ class = ' \\ name Dangerous'; // recommendation 6 $ class = "Dangerous \\ name"; // recommended