亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) namespace namespace in php

namespace namespace in php

Oct 22, 2016 am 12:00 AM

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 中有一個(gè)類 叫做Person,它放在一個(gè)叫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;//這個(gè)聲明要放在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">在同級(jí)目錄的test2.php中也有一個(gè)叫Person的類,它放在一個(gè)叫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;//這里就引用了這個(gè)shop命名空間。要使用test1.php中的類就必須寫上這行。as的作用是簡(jiǎn)寫命名空間的名字。
</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簡(jiǎn)寫,s指代命名空間shop。沒有用as的話就用 new shop\Person()來實(shí)例化對(duì)象。
</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();//不寫命名空間時(shí),就近在自己的命名空間里找Person類,找不到就報(bào)錯(cuò)咯。
</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>

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72