php中双冒号的应用

来源:互联网 发布:什么是软件原型 编辑:程序博客网 时间:2024/05/16 17:47

php类代码中常看到"::"的操作符,这个是作用域限定操作符,是用一个双冒号"::"表示,它用来置顶类中不同作用域的级别。左边是作用域右边是访问作用域的成员。

在php中定义的作用域有self和parent两种(在php6中提供了static作用域)。

 

self:表示当前类的作用域,与this不同的是它不表示类的某个特定实例,在类之外的代码中不能使用self,而且它不能识别自己在继承中层次的位置。也就是说,当在扩展类中使用self时,它调用的不是父类的方法,而是扩展类的重载的方法。


parent:表示当前类父类的作用域,其余的跟self特性一样。


举例说明php双冒号::操作符:

[php] view plaincopyprint?
  1. <?php  
  2. class forasp{  
  3.   static $url="http://blog.csdn.net/abandonship";  
  4.   static $webname = "PHP学习之双冒号的用法";  
  5.   public function writeurl(){  
  6.     echo self::$url;//调用自己的内容  
  7.   }  
  8.   public function writewebname(){  
  9.     echo "测试子类调用父类内容";  
  10.   }  
  11. }  
  12.   
  13. class cn extends forasp{  
  14.   function father(){  
  15.     parent::wirtewebname();  
  16.   }  
  17. }  
  18.   
  19. $a = new forasp();//实例化父类  
  20. $a->writeurl();//调用自身内容  
  21. $b = new cn();  
  22. $b->writewebname();//调用父类内容  
  23. ?>  

 

在调用静态方法中也可以使用::来调用类中的静态方法或者属性,这样可以减少资源使用,因为每个类的实例都会占有一部分资源。


php6中提出static::作用域,是我们不再需要self::和parent::。希望指向最终的实现功能的类时,就用static::,这个限定符会在代码执行前立即计算出继承层中最后那个类的成员,这一过程叫做延迟绑定。

 

“双冒号操作符”也或称为“作用域限定操作符”(Scope Resolution Operator)可以访问静态、const和类中重写的属性与方法。
在类定义外使用的话,使用类名调用。在PHP 5.3.0,可以使用变量代替类名。


Program List:用变量在类定义外部访问

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

 

Program List:在类定义外部使用双冒号(::)

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

 

程序运行结果:

Fruit Color Red

 

Program List:调用parent方法

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

 

程序运行结果:
Fruit::showColor() 
Apple::showColor()

 

Program List:使用作用域限定符

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

 

程序运行结果:
Banana is yellow

Program List:调用基类的方法

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

 

程序运行结果:
show color

原创粉丝点击