Namenserklärung:
Namespace (Namespace), Namespace ist eine Funktion, die ab PHP5.3 unterstützt wird. Es gibt zwei Hauptfunktionen: 1. Es kann verhindern, dass der Klassenname zu lang wird. 2. Bei Verwendung in mehreren Frameworks kommt es zu keinem Konflikt zwischen Klassen mit demselben Namen.
Namespace, Sie können es anhand des Namens erkennen. Der Zweck besteht darin, ihn zu benennen. Warum nennen Sie ihn sonst nicht QQ Space, Inception oder Seventh Dimension Space? Ähm, da scheint da etwas Seltsames drin zu sein.
Hier ist der Beispielcode:
<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>