LaneBlog

蝼蚁虽小,也有梦想

PHP Socket服务 | PHP微信开发框架 | 开源博客

PHP的命名空间

Clicks: 3476 Date: 2014-06-08 12:51:45 Power By 李轩Lane

    PHP命名空间是PHP5.3开始支持。本篇讲解PHP命名空间用法和PHP命名空间详解。它的诞生使的我们在一个文件中可以使用多个同名的类而不冲突。
    好处:我们的项目有一个记录日志文件的类,叫做Log。然后我们又必须要引入另一个代码包,这个代码包里也有一个叫做Log的类。那么在一个文件中,我们记录日志的又需要给两个类都写一条日志。可以类同名了,怎么办?这个时候,命名空间应运而生。在Java等语言中命名空间是很早就已经提供了支持,而我大PHP一直到5.3才对命名空间提供了支持。
    示例一:
文件index.php
<?php
include 'test.php';

class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}
$obj = new index();
$obj->a();
echo '<br>';
$obj1 = new test\index();
$obj1->a();
?>
文件test.php
<?php
namespace test;
class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}
?>
我们给index.php不设置命名空间,对test.php设置命名空间,名为test。运行index.php。 结果:
index.php
index : index::a
test.php
test\index : test\index::a
我们看到了,同名的类也可以运行而不冲突了。 示例二: 文件index.php
<?php
namespace index;
include 'test.php';

class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}
$obj = new index();
$obj->a();
echo '<br>';
$obj1 = new \test\index();
$obj1->a();
?>
文件test.php
<?php
namespace test;
class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}
?>
我们给index.php设置命名空间,名为index,对test.php设置命名空间,名为test。运行index.php。 结果:
index.php
index\index : index\index::a
test.php
test\index : test\index::a
比较示例一和二,不对index.php设置命名空间,即该文件是整个PHP全局命名空间下面的一个文件,那么使用test\index()即可,如果对index.php设置命名空间,即在其他的命名空间中使用命名空间,就要多一个“\”,就要使用\test\index()。 示例三: 文件index.php
<?php
namespace index;

include 'namespace.php';

use \test\test1\test2 as test2;

class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}

$obj = new index();
$obj->a();

echo '<br>';

$obj1 = new \test\test1\test2\index();
$obj1->a();

echo '<br>';

$obj1 = new test2\index();
$obj1->a();
文件test.php
<?php
namespace test\test1\test2;
class index{
    public function a(){
        echo basename(__FILE__);
        echo '<br>';
        echo __CLASS__ . ' : ' . __METHOD__;
    }
}
结果:
index.php
index\index : index\index::a
test.php
test\test1\test2\index : test\test1\test2\index::a
test.php
test\test1\test2\index : test\test1\test2\index::a
这说明了什么?别名!用过SQL吧。
select COUNT(*) as `count` from `tebleName`
嗯,一个意思。\test\test1\test2这个名字太长了,就别名为test2就好了。使用了use之后呢,这个命名空间就想到于是在index这个命名空间下面了,而不是全局命名空间的一员了,所以使用test2\index(),而不是\test2\index()。 别名时在PHP代码编译的时候执行的,而变量的解析则要更晚。也就是说不能对变量运用use关键字。示例如下(摘自官方手册示例):
<?php
use My\Full\Classname as Another, My\Full\NSname;

$obj = new Another; // 实例化一个 My\Full\Classname 对象
$a = 'Another';
$obj = new $a;      // 实际化一个 Another 对象

Reply: Danielshirm On 2020-06-19 17:55:02

buy prescription drugs <a href=http://onlinepharmacyero.com/#>comfortis for dogs without vet prescription</a> http://onlinepharmacyero.com viagra sale online canada cialis buy cheap

回复

Reply: Danielshirm On 2020-06-22 16:27:54

buy prescription drugs from canada cheap <a href=http://onlinepharmacyero.com/#>real viagra without a doctor prescription usa</a> http://onlinepharmacyero.com viagra super active+ 100mg pills viagra buy in london

回复

Reply: Jamescog On 2020-06-23 17:22:29

expired cialis 3 years <a href=" http://bestpricemedz.com/# ">buy generic cialis</a> bestpricemedz.com cialis generic

回复

Reply: LarryZen On 2020-06-23 19:25:38

the cost of cialis <a href=" http://bestpricemedz.com/# ">buy cialis</a> http://bestpricemedz.com/# buy cialis cialis dosage 40 mg dangerous <a href=http://bestpricemedz.com/#>generic viagra</a> viagra on line

回复

Reply: JeromeAgile On 2020-06-24 03:48:42

http://bestpricemedz.com/# cialis vs viagra

回复

Reply: Jeromecoofs On 2020-06-24 23:59:22

lowest price cialis <a href=" http://bestpricemedz.com/# ">buy generic cialis</a> bestpricemedz.com viagra without doctor prescription

回复

Reply: Russellbub On 2020-06-25 01:40:43

safe alternatives to viagra and cialis <a href=" http://bestpricemedz.com/# ">generic viagra</a> http://bestpricemedz.com/# cialis

回复

Reply: Jeromecoofs On 2020-06-25 21:32:25

nose congested when taking cialis <a href=" http://bestpricemedz.com/# ">cialis without doctor prescription</a> bestpricemedz.com cheap cialis

回复

Reply: JeromeAgile On 2020-06-26 06:55:17

http://bestpricemedz.com/# generic cialis

回复

Reply: Dennisfealt On 2020-06-29 09:56:33

cialis prices <a href=" http://bbs.realsoft.cc/home.php?mod=space&uid=1277899 ">cialis</a> and <a href=" http://hzsn419.com/home.php?mod=space&uid=5344 ">generic cialis</a> and <a href=" http://aiam.7mry.com/member.php?u=168419 ">cialis coupon</a> and <a href=" http://www.babygoonline.com/home.php?mod=space&uid=138481 ">buy generic cialis</a> and <a href=" http://9mux.com/space-uid-300529.html ">cialis price</a> and <a href=" http://www.tjyou.cn/home.php?mod=space&uid=153575 ">cialis 20mg</a> http://www.fanduankou.com/space-uid-121428.html buy generic cialis

回复

Reply: JesusChuth On 2020-06-29 13:18:47

cialis without a doctor's prescription <a href=" http://jiuaiwm.com/home.php?mod=space&uid=924445 ">buy generic cialis</a> and <a href=" http://www.gpxxz.cn/space-uid-64657.html ">buy cialis</a> and <a href=" http://www.ccnau.cn/space-uid-156429.html ">buy cialis</a> and <a href=" http://tsaijia.com/space-uid-936420.html ">buy generic cialis</a> and <a href=" http://luntan.wangwangyutang.com/home.php?mod=space&uid=51160 ">buy cialis online</a> and <a href=" http://www.clownspiracy.com/forum/member.php?action=profile&uid=354 ">buy cialis</a> http://test.viczz.com/home.php?mod=space&uid=914181 cialis coupon

回复

Reply: AnthonyBuh On 2020-06-29 17:50:13

how to get cialis samples <a href=" http://forums.cysticfibrosis.com/member.php/73068-cufyeumc ">buy generic cialis</a> and <a href=" http://plus-3at.ir/members/117177-yxhxqjlu ">buy cialis online</a> and <a href=" http://www.nptm.ru/phpBB2/profile.php?mode=viewprofile&u=2678836 ">cialis coupon</a> and <a href=" http://bbs.bolaiedu.com/home.php?mod=space&uid=992218 ">generic cialis</a> and <a href=" http://www.csgpchina.com/home.php?mod=space&uid=36737 ">cialis coupon</a> and <a href=" http://www.xjjiazhuang.com/bbs/home.php?mod=space&uid=194982 ">buy cialis online</a> cialis or viagra <a href=" http://www.chiapaspictures.com/profile.php?uid=914872 ">cialis price</a> and <a href=" http://www.yue168.cn/home.php?mod=space&uid=201145 ">cialis coupon</a> and <a href=" https://coinchat.xyz/index.php?action=profile;u=47485 ">cialis online</a> and <a href=" http://dongpogou.com/home.php?mod=space&uid=1494361 ">buy cialis online</a> and <a href=" http://www.xmf168.com/home.php?mod=space&uid=440250 ">buy generic cialis</a> and <a href=" http://qa.site5.com/profile/wvejxakl ">buy cialis online</a> http://www.xiangpopo.com/bbs/home.php?mod=space&uid=174820 cialis coupon

回复

Reply: Victorjully On 2020-07-01 09:17:36

cialis 20 mg best price <a href=" http://tjztjy.com/home.php?mod=space&uid=962341 ">cialis</a> and <a href=" http://23.225.178.244/home.php?mod=space&uid=2907443 ">cialis 20mg</a> and <a href=" http://wg-travel.com/index.php?option=com_k2&view=itemlist&task=user&id=1510741 ">cialis coupon</a> and <a href=" http://www.zghaoqi.com/home.php?mod=space&uid=263881 ">cialis coupon</a> and <a href=" http://buy.cpu668.com/space-uid-303097.html ">cialis price</a> and <a href=" http://bellevuestellenbosch.co.za/forum/profile.php?id=630623 ">cialis 20mg</a> http://bbs.realsoft.cc/home.php?mod=space&uid=1280066 cialis

回复

Reply: ShannonHab On 2020-07-02 06:11:42

online cialis <a href=" http://tadalafilprof.com/# ">buy cialis</a> tadalafilprof.com - is generic cialis safe

回复

Reply: ArthurTop On 2020-07-02 18:41:34

http://tadalafilprof.com - legitimate cialis by mail [url=http://tadalafilprof.com]cialis coupon[/url]

回复

Reply: Jesusoxiny On 2020-07-02 20:24:51

is generic cialis safe <a href=" http://tadalafilprof.com/# ">cialis without doctor prescription</a> current cost of cialis 5mg cvs: http://tadalafilprof.com

回复

Reply: ArthurTop On 2020-07-04 18:23:16

http://tadalafilprof.com - cialis pills for sale [url=http://tadalafilprof.com]cialis[/url]

回复

Reply: Williamrab On 2020-07-04 21:13:13

free cialis medication for providers <a href=" http://tadalafilprof.com/# ">cialis prices</a> http://tadalafilprof.com - how long does it take cialis to take effect [url=http://tadalafilprof.com]cialis 20 mg[/url]

回复

Reply: Williamunard On 2020-07-05 21:11:47

<a href=" http://tadalafilprof.com/# ">cialis generic</a> http://tadalafilprof.com - cialis vidalista [url=http://tadalafilprof.com]buy cialis[/url]

回复

Reply: 青岛风月网 On 2020-07-06 22:11:28

<a href=http://www.qdspaw.com>青岛风月网男士SPA减压</a>

回复

Add Comment
©2014 www.lanecn.com , All rights reserved. Power By Li Xuan.  京ICP备14005030号