当前位置:首页 > PHP教程 > php高级应用 > 列表

关于PHP中self关键字的用法详解

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-22 09:33:06 浏览: 评论:0 

与 parent 、 static 以及 this 的区别

要想将彻底搞懂 self ,要与 parent 、 static 以及 this 区分开。以下分别做对比。

parent

self 与 parent 的区分比较容易: parent 引用父类/基类被隐盖的方法(或变量), self则引用自身方法(或变量)。例如构造函数中调用父类构造函数:

  1. class Base { 
  2.  
  3.     public function __construct() { 
  4.  
  5.         echo "Base contructor!", PHP_EOL; 
  6.  
  7.     } 
  8.  
  9. } 
  10.  
  11.  
  12.  
  13. class Child { 
  14.  
  15.     public function __construct() { 
  16.  
  17.         parent::__construct(); 
  18.  
  19.         echo "Child contructor!", PHP_EOL; 
  20.  
  21.     } 
  22.  
  23. } 
  24.  
  25.  
  26.  
  27. new Child; 
  28.  
  29. // 输出: 
  30.  
  31. // Base contructor! 
  32.  
  33. // Child contructor! 

static

static 常规用途是修饰函数或变量使其成为类函数和类变量,也可以修饰函数内变量延长其生命周期至整个应用程序的生命周期,但是其与 self 关联上是PHP 5.3以来引入的新用途:静态延迟绑定。

有了 static 的静态延迟绑定功能,可以在运行时动态确定归属的类,例如:

  1. class Base { 
  2.  
  3.     public function __construct() { 
  4.  
  5.         echo "Base constructor!", PHP_EOL; 
  6.  
  7.     } 
  8.  
  9.  
  10.  
  11.     public static function getSelf() { 
  12.  
  13.         return new self(); 
  14.  
  15.     } 
  16.  
  17.  
  18.  
  19.     public static function getInstance() { 
  20.  
  21.         return new static(); 
  22.  
  23.     } 
  24.  
  25.  
  26.  
  27.     public function selfFoo() { 
  28.  
  29.         return self::foo(); 
  30.  
  31.     } 
  32.  
  33.  
  34.  
  35.     public function staticFoo() { 
  36.  
  37.         return static::foo(); 
  38.  
  39.     } 
  40.  
  41.  
  42.  
  43.     public function thisFoo() { 
  44.  
  45.         return $this->foo(); 
  46.  
  47.     } 
  48.  
  49.  
  50.  
  51.     public function foo() { 
  52.  
  53.         echo  "Base Foo!", PHP_EOL; 
  54.  
  55.     } 
  56.  
  57. } 
  58.  
  59.  
  60.  
  61. class Child extends Base { 
  62.  
  63.     public function __construct() { 
  64.  
  65.         echo "Child constructor!", PHP_EOL; 
  66.  
  67.     } 
  68.  
  69.  
  70.  
  71.     public function foo() { 
  72.  
  73.         echo "Child Foo!", PHP_EOL; 
  74.  
  75.     } 
  76.  
  77. } 
  78.  
  79.  
  80.  
  81. $base = Child::getSelf(); 
  82.  
  83. $child = Child::getInstance(); 
  84.  
  85.  
  86.  
  87. $child->selfFoo(); 
  88.  
  89. $child->staticFoo(); 
  90.  
  91. $child->thisFoo(); 

输出结果如下:

关于PHP中self关键字的用法详解

在函数引用上, self 与 static 的区别是:

对于静态成员函数, self 指向代码当前类, static 指向调用类;对于非静态成员函数, self 抑制多态,指向当前类的函数, static 等同于 this ,动态指向调用类的函数。

parent 、 self 、 static 三个关键字联合在一起看挺有意思,分别指向父类、当前类、子类,有点“过去、现在、未来”的味道。

this

self 与 this 是被讨论最多,也是最容易引起误用的组合。

两者的主要区别如下:

1、this 不能用在静态成员函数中, self 可以;

2、对静态成员函数/变量的访问, 建议 用 self ,不要用 $this:: 或 $this-> 的形式;

3、对非静态成员变量的访问,不能用 self ,只能用 this ;

4、this 要在对象已经实例化的情况下使用, self 没有此限制;

5、在非静态成员函数内使用, self 抑制多态行为,引用当前类的函数;而 this 引用调用类的重写(override)函数(如果有的话)。

self 的用途

看完与上述三个关键字的区别, self 的用途是不是呼之即出?一句话总结,那就是: self总是指向“当前类(及类实例)”。详细说则是:

1、替代类名,引用当前类的静态成员变量和静态函数;

2、抑制多态行为,引用当前类的函数而非子类中覆盖的实现;

Tags: self关键字

分享到: