Home > Article > Backend Development > A brief introduction to the use of namespaces in PHP 5.3_PHP Tutorial
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
<ol class="dp-c"><li class="alt"><span><span><?php </SPAN></SPAN><LI class=""><SPAN>namespace Project::Module; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>class</FONT></STRONG></SPAN><SPAN> User { </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>const</FONT></STRONG></SPAN><SPAN> STATUS_OK = true; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>function</FONT></STRONG></SPAN><SPAN> register(</SPAN><SPAN class=vars><FONT color=#dd0000>$data</FONT></SPAN><SPAN>) { </SPAN></SPAN><LI class=""><SPAN>... </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>... </SPAN><LI class=""><SPAN>} </SPAN></LI></OL>
and then in the controller (maybe other files) you can call it like this
<OL class=dp-c><LI class=alt><SPAN><SPAN class=vars><FONT color=#dd0000>$user</FONT></SPAN><SPAN> = </SPAN><SPAN class=keyword><STRONG><FONT color=#006699>new</FONT></STRONG></SPAN><SPAN> Project::Module::User(); </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$user</FONT></SPAN><SPAN>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span></span></li></ol>
It is indeed no different from usual, but we can Connect two independent classes. For example,
<ol class="dp-c"> <li class="alt"><span><span>Project::Module::User; </span></span></li> <li class=""><span>Project::Module::Blog; </span></li> </ol>
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
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module; </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Module::User(); </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span> </li> </ol>
Even the constants in the
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module::User </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> ModuleUser; </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> ModuleUser; </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span> </li> </ol>
class can also be accessed through the namespace, such as in the above class STATUS_OK can be accessed through the namespace
<ol class="dp-c"><li class="alt"><span><span>Project::Module::User::STATUS_OK </span></span></li></ol>
. Furthermore, you can also use aliases to simplify such long "variable names"
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module::User::STATUS_OK </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> STATUS_OK; </span></span></li> <li class=""> <span></span><span class="func">echo</span><span> STATUS_OK; </span> </li> </ol>
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
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> foo() { </span></span></li> <li class=""><span>... </span></li> <li class="alt"><span>} </span></li> </ol>
can be executed using foo() or ::foo();.
Finally, use the autoload function to load the class in the specified namespace. A simple function is as follows:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> __autoload( </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ) { </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$classname</font></span><span> = </span><span class="func">strtolower</span><span>( </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ); </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$classname</font></span><span> = </span><span class="func">str_replace</span><span>( </span><span class="string"><font color="#0000ff">'::'</font></span><span>, DIRECTORY_SEPARATOR, </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ); </span> </li> <li class=""> <span></span><span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>( dirname( </span><span class="keyword"><strong><font color="#006699">__FILE__</font></strong></span><span> ) . </span><span class="string"><font color="#0000ff">'/'</font></span><span> . </span><span class="vars"><font color="#dd0000">$classname</font></span><span> . </span><span class="string"><font color="#0000ff">'.class.php'</font></span><span> ); </span> </li> <li class="alt"><span>} </span></li> </ol>
. For example, calling
<ol class="dp-c"><li class="alt"><span><span>__autoload(</span><span class="string"><font color="#0000ff">'Project::Module::User'</font></span><span>); </span></span></li></ol>
can automatically load the Project_Module_User.class.php file (although this seems inconvenient).