Home >Backend Development >PHP Tutorial >Namespace characteristics of several common languages
Namespace provides a way to organize classes logically and prevent naming conflicts.
Several common languages
C++
Namespaces can be nested
Nested namespaces refer to namespaces defined in other namespaces. A nested namespace is a nested scope. The name declared in the inner namespace will hide the members of the same name declared in the outer namespace:
<code><span>int</span> x = <span>20</span>; <span>namespace</span> outer { <span>int</span> x = <span>10</span>; <span>namespace</span> inner { <span>int</span> z = x; } } <span>int</span> main() { std::cout << outer::inner::z; <span>// 输出10 </span><span>return</span><span>0</span>; } </code>
C#
Nested namespace
In the namespace declaration Declare the namespace and separate each namespace with ".".
For example:
<code>namespace N1.N2 { class A {} class B {} } 在语义上等效于 namespace N1 { namespace N2 { class A {} class B {} } } </code>
Java
<code><span>package</span> cn.org.web3d.x3dpad</code>
The namespace in Java means that as long as you have an independent top-level domain name, you can ensure the absolute uniqueness of your project.
Objective-C
All class names in Objective-C applications must be globally unique. Naming has always been a weakness of Objective-C, compared with those elegant languages. Apple officially recommends that class names with two letters as prefixes are prepared for official libraries and frameworks. For third-party developers, the official recommendation is to use 3 or more letters as prefixes to name our classes.
PHP
<code><span>namespace</span> Vendor\Package\<span>...</span>..</code>
It emphasizes that the first-level Vendor should be a unique identifier, which means that you must have a top-level domain name of {Vendor}.com to ensure the absolute uniqueness of your project. For example, when I thought about this, I immediately registered a domain name of meanir.com to protect myself.
The above has introduced the namespace features of several common languages, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.