Home  >  Article  >  Backend Development  >  PHP 5.3 Features: Namespace_PHP Tutorial

PHP 5.3 Features: Namespace_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:52:56788browse

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      
<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>

and then in the controller (maybe other files) you can call it like this

12      
<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>

It's really the same as usual, but we can connect two independent classes. For example,

12      
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>

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      
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>

Even the constants in the

123      
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>

class can also be accessed through the namespace, such as in the above class STATUS_OK can be accessed through the namespace

1      
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>

. Furthermore, you can also use aliases to simplify such long "variable names"

12      
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;

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      
<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>

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      
<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>

. For example, calling

1      
__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>

can automatically load the Project_Module_User.class.php file

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371510.htmlTechArticleA new important 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. And this time again "...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn