php中雙冒號的應用
php類代碼中常看到"::"的操作符,這個是範圍限定操作符,是用一個雙冒號"::"表示,它用來置頂類中不同範圍的層級。左邊是範圍右邊是訪問範圍的成員。
在php中定義的範圍有self和parent兩種(在php6中提供了static範圍)。
self:表示當前類的範圍,與this不同的是它不表示類的某個特定執行個體,在類之外的代碼中不能使用self,而且它不能識別自己在繼承中層次的位置。也就是說,當在擴充類中使用self時,它調用的不是父類的方法,而是擴充類的重載的方法。
parent:表示當前類父類的範圍,其餘的跟self特性一樣。
舉例說明php雙冒號::操作符:
?
Php代碼
- class?forasp{ ??
- ??static?$url="http://blog.csdn.net/abandonship"; ??
- ??static?$webname?=?"PHP學習之雙冒號的用法"; ??
- ??public?function?writeurl(){ ??
- ????echo?self::$url;//調用自己的內容 ??
- ??} ??
- ??public?function?writewebname(){ ??
- ????echo?"測試子類調用父類內容"; ??
- ??} ??
- } ??
- ??
- class?cn?extends?forasp{ ??
- ??function?father(){ ??
- ????parent::wirtewebname(); ??
- ??} ??
- } ??
- ??
- $a?=?new?forasp();//執行個體化父類 ??
- $a->writeurl();//調用自身內容 ??
- $b?=?new?cn(); ??
- $b->writewebname();//調用父類內容 ??
- ?>??
?
在調用靜態方法中也可以使用::來調用類中的靜態方法或者屬性,這樣可以減少資源使用,因為每個類的執行個體都會佔有一部分資源。
php6中提出static::範圍,是我們不再需要self::和parent::。希望指向最終的實現功能的類時,就用static::,這個限定符會在代碼執行前立即計算出繼承層中最後那個類的成員,這一過程叫做延遲綁定。
“雙冒號操作符”也或稱為“範圍限定操作符”(Scope Resolution Operator)可以訪問靜態、const和類中重寫的屬性與方法。
在類定義外使用的話,使用類名調用。在PHP 5.3.0,可以使用變數代替類名。
Program List:用變數在類定義外部存取
Php代碼
- class?Fruit?{ ??
- ??const?CONST_VALUE?=?'Fruit?Color'; ??
- } ??
- $classname?=?'Fruit'; ??
- echo?$classname::CONST_VALUE;?//?As?of?PHP?5.3.0 ??
- echo?Fruit::CONST_VALUE; ??
- ?>??
?
Program List:在類定義外部使用雙冒號(::)
Php代碼
- class?Fruit?{ ??
- ??const?CONST_VALUE?=?'Fruit?Color'; ??
- } ??
- class?Apple?extends?Fruit ??
- { ??
- ??public?static?$color?=?'Red'; ??
- ??public?static?function?doubleColon()?{ ??
- ????echo?parent::CONST_VALUE?.?"\n"; ??
- ????echo?self::$color?.?"\n"; ??
- ??} ??
- } ??
- Apple::doubleColon(); ??
- ?>??
?
程式運行結果:
Fruit Color Red
Program List:調用parent方法
?
Php代碼
- class?Fruit ??
- { ??
- ????protected?function?showColor()?{ ??
- ????????echo?"Fruit::showColor()\n"; ??
- ????} ??
- } ??
- ??
- class?Apple?extends?Fruit ??
- { ??
- ????//?Override?parent's?definition ??
- ????public?function?showColor() ??
- ????{ ??
- ????????//?But?still?call?the?parent?function ??
- ????????parent::showColor(); ??
- ????????echo?"Apple::showColor()\n"; ??
- ????} ??
- } ??
- ??
- $apple?=?new?Apple(); ??
- $apple->showColor(); ??
- ?>??
?
程式運行結果:
Fruit::showColor()
Apple::showColor()
?
Program List:使用範圍限定符
?
Php代碼
- ????class?Apple ??
- ????{ ??
- ????????public?function?showColor() ??
- ????????{ ??
- ????????????return?$this->color; ??
- ????????} ??
- ????} ??
- ????class?Banana ??
- ????{ ??
- ????????public?$color; ??
- ????????public?function?__construct() ??
- ????????{ ??
- ????????????$this->color?=?"Banana?is?yellow"; ??
- ????????} ??
- ????????public?function?GetColor() ??
- ????????{ ??
- ????????????return?Apple::showColor(); ??
- ????????} ??
- ????} ??
- ????$banana?=?new?Banana; ??
- ????echo?$banana->GetColor(); ??
- ?>??
?
程式運行結果:
Banana is yellow
Program List:調用基類的方法
Php代碼
- ??
- class?Fruit ??
- { ??
- ????static?function?color() ??
- ????{ ??
- ????????return?"color"; ??
- ????} ??
- ??
- ????static?function?showColor() ??
- ????{ ??
- ????????echo?"show?"?.?self::color(); ??
- ????} ??
- } ??
- ??
- class?Apple?extends?Fruit ??
- { ??
- ????static?function?color() ??
- ????{ ??
- ????????return?"red"; ??
- ????} ??
- } ??
- ??
- Apple::showColor(); ??
- //?output?is?"show?color"! ??
- ??
- ?>??
?
程式運行結果:
show color