This article mainly introduces php namespace related questions and skills. For more information, see 1. what is a namespace?
A namespace is a special scope that contains identifiers in this scope and is also an identifier. You can map the namespace to the operating system directory. A namespace is equivalent to a directory. the classes, functions, and constants in the namespace are equivalent to files in the directory. The file names in the same directory (namespace) cannot be the same, but different directories can have files with the same name.
2. what problems does namespace solve?
Resolve name conflicts. for example, a class is defined. this class is exactly the same as the class in the PHP internal class or the class in the include class library.
To improve code readability, the namespace has an alias function, which can help you create an alias for a class name that can contain up to a dozen characters, thus shortening the code and avoiding conflicts with the names of other spaces.
3. which code will be affected by the namespace.
Three types: Class, function, and constant. Only the three of them are affected. what should they do. Speaking of constants, php 5.3 and later can use the const keyword to define constants. before 5.3, define is used. the namespace is only valid for the const keyword.
4. how to define a namespace
The code is as follows:
Namespace MyProject;
Const CONNECT_ OK = 1; // after php5.3
Class Connection {/*...*/}
Function connect (){/*...*/}
# Example 2
Namespace MyProjectSubLevel;
Const CONNECT_ OK = 1; // after php5.3
Class Connection {/*...*/}
Function connect (){/*...*/}
You can use the 'namespace name' to declare a space. before namespace, except for the declare statement, there cannot be any other php statements, no non-php code, and no space.
The following is an error:
The code is as follows:
$ A = 1;
Namespace MyProject;
?> Www.jb51.net
// Fatal error: Namespace declaration statement has to be the very first statement in the script...
In addition, the same namespace can be defined in multiple files, which is very useful for the organizational framework. Files starting with the same namespace MyProject; are the same namespace. Therefore, do not have the same class, function, or constant names between files.
Of course, the same file can also define multiple namespaces, but this is not recommended. (Learn how to define multiple namespaces in the same file)
5. how to use a namespace
The namespace can be used in three forms:
. Non-qualified name -- no delimiter is used, and class/function/constant names are used directly, for example, new Foo (); foo (); echo FOO; when a file uses a namespace,
The code is as follows:
Namespace MyObject;
New Foo (); // call MyObjectFoo ();
Foo (); // call MyObjectFoo ();
Echo FOO; // call MyObjectFOO;
Non-fully qualified name-not starting with a separator, such as new SubFoo.
The code is as follows:
Namespace MyObject; new SubFoo (); // call MyObjectSubFoo ();
Fully qualified name-the method starting with a separator, which is equivalent to an absolute address in the operating system. Such as new OtherNSFoo ();
The code is as follows:
Namespace MyObject; new OtherNSFoo (); // call OtherNsFoo (); regardless of the MyObject namespace.
Tip: For functions and constants, there is a special place (reserve global functions/constants ).
The code is as follows:
Namespace MyObject;
Funcname (); // If MyObjectFuncname exists, call MyObjectFuncname (); otherwise, try to call funcname (); echo FOO; // same as above.
For classes, there is also a special place.
The code is as follows:
Namespace MyObject;
New Foo (); // * if MyObjectFoo exists, call it. if not, call _ autoload to load the MyObjectFoo class.
// Note that classes in the global scope will not be automatically called.
As mentioned earlier, the namespace also has a purpose-to take aliases.
The code is as follows:
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 namespace
Dynamic is always confusing, but it brings flexibility. Namespaces can also use dynamic language features, but note that the calling of namespaces directly is resolved during compilation, while dynamic features are not resolved during compilation. Therefore, you must add a prefix. For example:
The code is as follows:
Namespace MyObjectSub;
New Foo (); // call MyObjectSubFoo (), which is resolved to MyObjectSubFoo at compilation.
$ A = 'foo ';
New $ a (); // call Foo () instead of MyObjectSubFoo ()
$ B = 'myobjectsubfoo'; // equivalent to MyObjectSubFoo
New $ B (); // call MyObjectSubFoo ()
// If double quotation marks are used, use \, for example, $ a = "\ MyObject \ Sub ";
Appendix 1: The same file defines multiple namespaces
There are two methods:
The code is as follows:
Namespace MyProject;
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
Namespace AnotherProject;
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
Method 1: record the journal account.
The code is as follows:
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 2: Use braces to put the code of the same namespace in braces. This method requires that no code except declare should be included in the braces. The code in the global scope is surrounded by braces without a space name.