php中雙冒號的施用

來源:互聯網
上載者:User
php中雙冒號的應用

php類代碼中常看到"::"的操作符,這個是範圍限定操作符,是用一個雙冒號"::"表示,它用來置頂類中不同範圍的層級。左邊是範圍右邊是訪問範圍的成員。

在php中定義的範圍有self和parent兩種(在php6中提供了static範圍)。

self:表示當前類的範圍,與this不同的是它不表示類的某個特定執行個體,在類之外的代碼中不能使用self,而且它不能識別自己在繼承中層次的位置。也就是說,當在擴充類中使用self時,它調用的不是父類的方法,而是擴充類的重載的方法。


parent:表示當前類父類的範圍,其餘的跟self特性一樣。


舉例說明php雙冒號::操作符:

?

Php代碼

  1. class?forasp{ ??
  2. ??static?$url="http://blog.csdn.net/abandonship"; ??
  3. ??static?$webname?=?"PHP學習之雙冒號的用法"; ??
  4. ??public?function?writeurl(){ ??
  5. ????echo?self::$url;//調用自己的內容 ??
  6. ??} ??
  7. ??public?function?writewebname(){ ??
  8. ????echo?"測試子類調用父類內容"; ??
  9. ??} ??
  10. } ??
  11. ??
  12. class?cn?extends?forasp{ ??
  13. ??function?father(){ ??
  14. ????parent::wirtewebname(); ??
  15. ??} ??
  16. } ??
  17. ??
  18. $a?=?new?forasp();//執行個體化父類 ??
  19. $a->writeurl();//調用自身內容 ??
  20. $b?=?new?cn(); ??
  21. $b->writewebname();//調用父類內容 ??
  22. ?>??

?

在調用靜態方法中也可以使用::來調用類中的靜態方法或者屬性,這樣可以減少資源使用,因為每個類的執行個體都會佔有一部分資源。


php6中提出static::範圍,是我們不再需要self::和parent::。希望指向最終的實現功能的類時,就用static::,這個限定符會在代碼執行前立即計算出繼承層中最後那個類的成員,這一過程叫做延遲綁定。

“雙冒號操作符”也或稱為“範圍限定操作符”(Scope Resolution Operator)可以訪問靜態、const和類中重寫的屬性與方法。
在類定義外使用的話,使用類名調用。在PHP 5.3.0,可以使用變數代替類名。


Program List:用變數在類定義外部存取

Php代碼

  1. class?Fruit?{ ??
  2. ??const?CONST_VALUE?=?'Fruit?Color'; ??
  3. } ??
  4. $classname?=?'Fruit'; ??
  5. echo?$classname::CONST_VALUE;?//?As?of?PHP?5.3.0 ??
  6. echo?Fruit::CONST_VALUE; ??
  7. ?>??

?

Program List:在類定義外部使用雙冒號(::)

Php代碼

  1. class?Fruit?{ ??
  2. ??const?CONST_VALUE?=?'Fruit?Color'; ??
  3. } ??
  4. class?Apple?extends?Fruit ??
  5. { ??
  6. ??public?static?$color?=?'Red'; ??
  7. ??public?static?function?doubleColon()?{ ??
  8. ????echo?parent::CONST_VALUE?.?"\n"; ??
  9. ????echo?self::$color?.?"\n"; ??
  10. ??} ??
  11. } ??
  12. Apple::doubleColon(); ??
  13. ?>??

?

程式運行結果:

Fruit Color Red

Program List:調用parent方法

?

Php代碼

  1. class?Fruit ??
  2. { ??
  3. ????protected?function?showColor()?{ ??
  4. ????????echo?"Fruit::showColor()\n"; ??
  5. ????} ??
  6. } ??
  7. ??
  8. class?Apple?extends?Fruit ??
  9. { ??
  10. ????//?Override?parent's?definition ??
  11. ????public?function?showColor() ??
  12. ????{ ??
  13. ????????//?But?still?call?the?parent?function ??
  14. ????????parent::showColor(); ??
  15. ????????echo?"Apple::showColor()\n"; ??
  16. ????} ??
  17. } ??
  18. ??
  19. $apple?=?new?Apple(); ??
  20. $apple->showColor(); ??
  21. ?>??

?

程式運行結果:
Fruit::showColor()
Apple::showColor()

?

Program List:使用範圍限定符

?

Php代碼

  1. ????class?Apple ??
  2. ????{ ??
  3. ????????public?function?showColor() ??
  4. ????????{ ??
  5. ????????????return?$this->color; ??
  6. ????????} ??
  7. ????} ??
  8. ????class?Banana ??
  9. ????{ ??
  10. ????????public?$color; ??
  11. ????????public?function?__construct() ??
  12. ????????{ ??
  13. ????????????$this->color?=?"Banana?is?yellow"; ??
  14. ????????} ??
  15. ????????public?function?GetColor() ??
  16. ????????{ ??
  17. ????????????return?Apple::showColor(); ??
  18. ????????} ??
  19. ????} ??
  20. ????$banana?=?new?Banana; ??
  21. ????echo?$banana->GetColor(); ??
  22. ?>??

?

程式運行結果:
Banana is yellow

Program List:調用基類的方法

Php代碼

  1. ??
  2. class?Fruit ??
  3. { ??
  4. ????static?function?color() ??
  5. ????{ ??
  6. ????????return?"color"; ??
  7. ????} ??
  8. ??
  9. ????static?function?showColor() ??
  10. ????{ ??
  11. ????????echo?"show?"?.?self::color(); ??
  12. ????} ??
  13. } ??
  14. ??
  15. class?Apple?extends?Fruit ??
  16. { ??
  17. ????static?function?color() ??
  18. ????{ ??
  19. ????????return?"red"; ??
  20. ????} ??
  21. } ??
  22. ??
  23. Apple::showColor(); ??
  24. //?output?is?"show?color"! ??
  25. ??
  26. ?>??

?

程式運行結果:
show color

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.