An important new feature of PHP 5.3 is namespace.
This feature was proposed in PHP5.0x, but was later canceled and scheduled to be implemented in PHP6. This time, PHP 5.3 was released "ahead of schedule" again, which shows that developers attach great importance to it and are cautious.
The content of the document may be out of date when it is officially released (documentation maybe out dated), so here is a brief explanation of the usage of namespace: first, declare a namespace, add the new keyword namespace, and It should be at the beginning of the class file
12345678 Copy after login | <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)"><?php</SPAN> namespace Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">class</SPAN> User <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">const</SPAN> STATUS_OK <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">true</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</SPAN> register<SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,136)">$data</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN> <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="COLOR: rgb(51,153,51)">...</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> <SPAN style="COLOR: rgb(51,153,51)">...</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">?></span> Copy after login |
and then in the controller (maybe other files) you can call it like this
12 Copy after login Copy after login Copy after login | <span style="COLOR: rgb(0,0,136)">$user</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</span> Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$user</span><span style="COLOR: rgb(51,153,51)">-></span><span style="COLOR: rgb(0,64,0)">register</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$register_info</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> Copy after login |
It's really the same as usual, but we can connect two independent classes. For example,
12 Copy after login Copy after login Copy after login | Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span><span style="COLOR: rgb(51,153,51)">;</span> Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Blog</span><span style="COLOR: rgb(51,153,51)">;</span> Copy after login |
makes it easier to describe and understand the relationship between variables and classes from the language itself, thereby avoiding the "traditional" lengthy naming method of Project_Module_Blog.
The above description may be difficult to explain the benefits of using namespaces. The newly added use and as keywords may explain the problem better. Use and as statements can reference and declare namespace "aliases". For example, the code for instantiating the class in the above controller can be written like this
123 Copy after login Copy after login Copy after login | use Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$user</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</span> Module<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$user</span><span style="COLOR: rgb(51,153,51)">-></span><span style="COLOR: rgb(0,64,0)">register</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$register_info</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> Copy after login |
Even the constants in the
123 Copy after login Copy after login Copy after login | use Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span> <span style="COLOR: rgb(177,177,0)">as</span> ModuleUser<span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$user</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</span> ModuleUser<span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$user</span><span style="COLOR: rgb(51,153,51)">-></span><span style="COLOR: rgb(0,64,0)">register</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$register_info</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> Copy after login |
class can also be accessed through the namespace, such as in the above class STATUS_OK can be accessed through the namespace
1 Copy after login Copy after login | Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">STATUS_OK</span> Copy after login |
. Furthermore, you can also use aliases to simplify such long "variable names"
12 Copy after login Copy after login Copy after login | use Project<span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">Module</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">User</span><span style="COLOR: rgb(51,153,51)">::</span><span style="COLOR: rgb(0,64,0)">STATUS_OK</span> as STATUS_OK; echo STATUS_OK; Copy after login |
By the way, mention the concept of "Hyperspace (The Global Namespace)". The so-called "hyperspace" refers to variables, classes and functions that do not have a designated namespace. For example, functions like
123 Copy after login Copy after login Copy after login | <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</span> foo<span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(0,153,0)">{</span> <span style="COLOR: rgb(51,153,51)">...</span> <span style="COLOR: rgb(0,153,0)">}</span> Copy after login |
can be executed using foo() or ::foo();.
Finally, use the autoload function to load the class in the specified namespace. The simple function is as follows
12345 Copy after login | <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</span> __autoload<span style="COLOR: rgb(0,153,0)">(</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(0,153,0)">{</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">strtolower</span><span style="COLOR: rgb(0,153,0)">(</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">str_replace</span><span style="COLOR: rgb(0,153,0)">(</span> <span style="COLOR: rgb(0,0,255)">'::'</span><span style="COLOR: rgb(51,153,51)">,</span> DIRECTORY_SEPARATOR<span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(177,177,0)">require_once</span><span style="COLOR: rgb(0,153,0)">(</span> <span style="COLOR: rgb(153,0,0)">dirname</span><span style="COLOR: rgb(0,153,0)">(</span> <span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">__FILE__</span> <span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(51,153,51)">.</span> <span style="COLOR: rgb(0,0,255)">'/'</span> <span style="COLOR: rgb(51,153,51)">.</span> <span style="COLOR: rgb(0,0,136)">$classname</span> <span style="COLOR: rgb(51,153,51)">.</span> <span style="COLOR: rgb(0,0,255)">'.class.php'</span> <span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> <span style="COLOR: rgb(0,153,0)">}</span> Copy after login |
. For example, calling
1 Copy after login Copy after login | __autoload<span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,255)">'Project::Module::User'</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span> Copy after login |
can automatically load the Project_Module_User.class.php file