2, using namespaces to solve what problem?
. Resolve name conflicts, such as defining a class, which is exactly the same as the class in PHP or a class library included in.
. To improve code readability, namespaces have an alias feature that helps you alias a more than 10-character class name to shorten the code without worrying about naming conflicts with other spaces.
3, which code will be affected by the namespace.
Three classes: class, function, constant. Only their brothers are affected, the others do, and why. When it comes to constants, PHP 5.3 can then use the Const keyword to define constants, 5.3 This uses define before, and the namespace is only valid for the Const keyword.
4, how to define the namespace
The code is as follows |
Copy Code |
namespace MyProject; Const CONNECT_OK = 1;//php5.3 After Class Connection {/* ... */} function connect () {//* ... */} #例子二 namespace Myprojectsublevel; Const CONNECT_OK = 1;//php5.3 After Class Connection {/* ... */} function connect () {//* ... */} |
Use ' namespace space name ' to declare a space, before namespace in addition to declare statements can not have any other PHP statements, but also can not have any non-PHP code, even the space can not have.
The following is the wrong form
The code is as follows |
Copy Code |
$a = 1; namespace MyProject; ? >www.111cn.net Fatal Error:namespace declaration statement has to be the very the script ... |
In addition, the same namespace can be defined in multiple files, which is useful for the organizational framework. Files that start with the same namespace MyProject, which are the same namespace. So note that the file may not have the same class/function/constant name.
Of course, the same file can also define multiple namespaces, but it is highly recommended. (Understanding the same file defines multiple namespaces)
5, how to use the namespace
Namespaces have three forms of use:
. Unqualified name-use no delimiter, directly using class/function/constant name, such as: New Foo (); Foo (); Echo FOO; When a file has a namespace used,
The code is as follows |
Copy Code |
<?php namespace MyObject; New Foo (); Call Myobjectfoo (); Foo (); Call Myobjectfoo (); Echo FOO; Call Myobjectfoo; |
. Non-fully qualified name-not beginning with a delimiter, such as New Subfoo (); This form is the same as the unqualified name method.
The code is as follows |
Copy Code |
<?php namespace MyObject; New Subfoo (); Call Myobjectsubfoo (); |
. Fully qualified name-a way to begin with a separator, equivalent to an absolute address in an operating system. such as new Othernsfoo ();
The code is as follows |
Copy Code |
<?php namespace MyObject; New Othernsfoo (); Call Othernsfoo (); Regardless of the MyObject namespace. |
Tip: There is also a special place for functions and constants (fallback global functions/constants).
The code is as follows |
Copy Code |
<?php namespace MyObject; FuncName (); Call Myobjectfuncname () if the myobjectfuncname exists, or try to invoke funcname (); Echo FOO; Ditto. |
For classes, there is also a special place.
The code is as follows |
Copy Code |
<?php namespace MyObject; New Foo (); * If Myobjectfoo exists, call it if it does not exist, call __autoload try to load the Myobjectfoo class in. |
Note that for classes it is not automatic to invoke a class under the global scope. */
Previously said, namespaces also have a purpose--alias.
The code is as follows |
Copy Code |
namespace MyObject; Use Othernssub as other; Use OTHERNSSUB2; Equivalent to use OTHERNSSUB2 as SUB2; Use/myclass; New Foo (); Call Myobjectfoo (); New Otherfoo (); Call Othernssubfoo (); New Sub2foo (); Call Othernssub2foo (); New MyClass (); Call MyClass (); |
6. Dynamic namespaces
Dynamic can always be confusing, yet it brings flexibility. Namespaces can also use dynamic language features, but note that because a direct call to a namespace is a compile-time resolution, dynamic features are not compile-time parsing. So be sure to prefix it. Such as:
The code is as follows |
Copy Code |
namespace Myobjectsub; New Foo (); Call Myobjectsubfoo (), which has been parsed into myobjectsubfoo at compile time $a = ' Foo '; New $a (); The call is foo (), not Myobjectsubfoo () $b = ' Myobjectsubfoo '; Equivalent to Myobjectsubfoo New $b (); Call Myobjectsubfoo () If you use double quotes, use \, such as $a = "\myobject\sub"; |
Appendix 1: The same file defines multiple namespaces
There are two kinds of methods:
The code is as follows |
Copy Code |
namespace MyProject; Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... */} namespace Anotherproject; Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... */} |
Method One, record the water account.
The code is as follows |
Copy Code |
Namespace MyProject { Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... */} } Namespace Anotherproject { Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... */} } namespace {//Global Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... */} } |
Method Two, use curly braces to place the same namespace code in braces. This method requires that there be no code other than declare outside the curly braces. For global scope code to surround with braces with no space name