PHP 5.3以上版本新增的一些方法
生活随笔
收集整理的這篇文章主要介紹了
PHP 5.3以上版本新增的一些方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
__FILE__用來指向當前執行的PHP腳本 ?$dir = dirname(__FILE__);
PHP5.3新增了一個魔術常量(magic constant)__DIR__執行當前執行的PHP腳本所在目錄
?
use關鍵字:給一個外部引用起別名。這是命名空間的一個重要特性,它同基于unix的文件系統的為文件或者目錄創建連接標志相類似
PHP命名空間支持三種別名方式(引用)
1、為一個類取別名
2、為一個接口取別名
3、為一個命名空間取別名
這三種方式都是用use關鍵字來完成。下面是這三種別名的分別舉例:
<?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?>注意的一點是,對于已命名的名字,全稱就包含了分隔符,比如 Foo\Bar,而不能用FooBar,而“\Foo\Bar”這個頭部的"\"是沒必要的,也不建議這樣寫。引入名必須是全稱,并且跟當前命名空間沒有程序上的關聯。
?
PHP也可以在同一行上申明多個,等同于上面的寫法
<?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?>?
還有值得一說的是,引入是在編譯時執行的,因此,別名不會影響動態類,例如:
<?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',編譯的時候,就將$a 定位到 Classname 了。
?
總結
以上是生活随笔為你收集整理的PHP 5.3以上版本新增的一些方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 股票账户的钱什么时候能转出来
- 下一篇: 协创数据是做什么的 两大主营业务介绍