Home > php教程 > php手册 > body text

namespace namespace in php

WBOY
Release: 2016-10-22 00:00:09
Original
1161 people have browsed it

Name explanation:

namespace (namespace), namespace is a feature supported starting from php5.3. There are two main functions: 1. It can prevent the class name from being too long. 2. When used in multiple frameworks, there will be no conflict between classes with the same name.

Namespace, as you can tell by looking at the name, the purpose is just for naming, otherwise why not call it QQ Space, Inception, or Seventh Dimension Space. Um, there seems to be something strange mixed in.


Here is the sample code:

<span style="color: #008080"> 1</span> <span style="color: #008000">//</span><span style="color: #008000">在test1.php 中有一个类 叫做Person,它放在一个叫shop的namespace里。</span>
<span style="color: #008080"> 2</span> <?<span style="color: #000000">php
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    namespace shop;//这个声明要放在php文件的最上面。就算是header也要让路。
</span><span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span>     <span style="color: #008080">header</span>('content-type:text/html;charset=utf-8'<span style="color: #000000">);
</span><span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span>     <span style="color: #0000ff">class</span><span style="color: #000000"> Person {
</span><span style="color: #008080"> 8</span>       <span style="color: #0000ff">public</span> <span style="color: #800080">$name</span> = 'Leonard'<span style="color: #000000">;
</span><span style="color: #008080"> 9</span> <span style="color: #000000">    }
</span><span style="color: #008080">10</span> ?>
<span style="color: #008080">11</span> <span style="color: #008000">//</span><span style="color: #008000">在同级目录的test2.php中也有一个叫Person的类,它放在一个叫admin的namespace中</span>
<span style="color: #008080">12</span> <?<span style="color: #000000">php
</span><span style="color: #008080">13</span> 
<span style="color: #008080">14</span> <span style="color: #000000">    namespace admin;//上面可以有空白行。别的都不行
</span><span style="color: #008080">15</span> 
<span style="color: #008080">16</span>     <span style="color: #0000ff">include_once</span>('./test.php'<span style="color: #000000">);
</span><span style="color: #008080">17</span> 
<span style="color: #008080">18</span>     <span style="color: #0000ff">use</span> shop <span style="color: #0000ff">as</span><span style="color: #000000"> s;//这里就引用了这个shop命名空间。要使用test1.php中的类就必须写上这行。as的作用是简写命名空间的名字。
</span><span style="color: #008080">19</span> 
<span style="color: #008080">20</span>     <span style="color: #0000ff">class</span><span style="color: #000000"> Person {
</span><span style="color: #008080">21</span>       <span style="color: #0000ff">public</span> <span style="color: #800080">$name</span> = 'Sheldon'<span style="color: #000000">;
</span><span style="color: #008080">22</span> <span style="color: #000000">    }
</span><span style="color: #008080">23</span> 
<span style="color: #008080">24</span>     <span style="color: #800080">$p1</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> s\Person();//上面用了as简写,s指代命名空间shop。没有用as的话就用 new shop\Person()来实例化对象。
</span><span style="color: #008080">25</span>     <span style="color: #0000ff">echo</span> <span style="color: #800080">$p1</span>->name;<span style="color: #008000">//</span><span style="color: #008000">Leonard</span>
<span style="color: #008080">26</span> 
<span style="color: #008080">27</span>     <span style="color: #800080">$p2</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Person();//不写命名空间时,就近在自己的命名空间里找Person类,找不到就报错咯。
</span><span style="color: #008080">28</span>     <span style="color: #0000ff">echo</span> <span style="color: #800080">$p2</span>->name;<span style="color: #008000">//</span><span style="color: #008000">Sheldon<br></span>
Copy after login

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!