PHP 5.3以上版本新增的一些方法
__FILE__用來指向當(dāng)前執(zhí)行的PHP腳本 ?$dir = dirname(__FILE__);
PHP5.3新增了一個(gè)魔術(shù)常量(magic constant)__DIR__執(zhí)行當(dāng)前執(zhí)行的PHP腳本所在目錄
?
use關(guān)鍵字:給一個(gè)外部引用起別名。這是命名空間的一個(gè)重要特性,它同基于unix的文件系統(tǒng)的為文件或者目錄創(chuàng)建連接標(biāo)志相類似
PHP命名空間支持三種別名方式(引用)
1、為一個(gè)類取別名
2、為一個(gè)接口取別名
3、為一個(gè)命名空間取別名
這三種方式都是用use關(guān)鍵字來完成。下面是這三種別名的分別舉例:
<?phpnamespace?foo;use?My\Full\Classname?as?Another;//?this?is?the?same?as?use?My\Full\NSname?as?NSnameuse?My\Full\NSname;//?importing?a?global?classuse?ArrayObject;$obj?=?new?namespace\Another;?//?instantiates?object?of?class?foo\Another$obj?=?new?Another;?//?instantiates?object?of?class?My\Full\ClassnameNSname\subns\func();?//?calls?function?My\Full\NSname\subns\func$a?=?new?ArrayObject(array(1));?//?instantiates?object?of?class?ArrayObject//?without?the?"use?ArrayObject"?we?would?instantiate?an?object?of?class?foo\ArrayObject?>注意的一點(diǎn)是,對于已命名的名字,全稱就包含了分隔符,比如 Foo\Bar,而不能用FooBar,而“\Foo\Bar”這個(gè)頭部的"\"是沒必要的,也不建議這樣寫。引入名必須是全稱,并且跟當(dāng)前命名空間沒有程序上的關(guān)聯(lián)。
?
PHP也可以在同一行上申明多個(gè),等同于上面的寫法
<?phpuse?My\Full\Classname?as?Another,?My\Full\NSname;$obj?=?new?Another;?//?instantiates?object?of?class?My\Full\ClassnameNSname\subns\func();?//?calls?function?My\Full\NSname\subns\func?>?
還有值得一說的是,引入是在編譯時(shí)執(zhí)行的,因此,別名不會(huì)影響動(dòng)態(tài)類,例如:
<?phpuse?My\Full\Classname?as?Another,?My\Full\NSname;$obj?=?new?Another;?//?instantiates?object?of?class?My\Full\Classname$a = 'Another';$obj = New $a; ? ? // instantiates object of class Another?>?
這里由于給變量$a 賦值了 'Another',編譯的時(shí)候,就將$a 定位到 Classname 了。
?
總結(jié)
以上是生活随笔為你收集整理的PHP 5.3以上版本新增的一些方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 股票账户的钱什么时候能转出来
- 下一篇: PHP5.2至5.6的新增功能详解