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

PHP Class self 与 static 异同与使用详解

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-14 10:51:32 浏览: 评论:0 

这篇文章主要介绍了PHP中 Class self 与 static 有什么区别,都怎么用,需要的朋友们下面随着小编来一起学习学习吧。

对于大多数 PHPer 来说,self 与 static 两个 PHP 关键词都不算陌生。我们学会通过self::xxxx这种方式来调用当前类的静态属性和方法。而 static 呢?想必很多人只知道它是用于定义一个静态方法和类属性关键词。

这也是我之前的认知。

现在我们来回顾一下这两个关键词的一些常见用法:

  1. // self 用法 1 :调用静态成员属性 
  2.  
  3.  
  4. class Person 
  5.  
  6. { 
  7.  
  8.     protected static $maxAddressCount = 5; // 收获地址创建最大数量。 
  9.  
  10.    
  11.  
  12.     public function test() 
  13.  
  14.     { 
  15.  
  16.         echo self::$maxAddressCount; 
  17.  
  18.     } 
  19.  
  20. } 
  21.  
  22.    
  23.  
  24. $person = new Person(); 
  25.  
  26. $person->test(); 
  27.  
  28. // self 用法 2 :调用静态方法 
  29.  
  30.  
  31. class Person 
  32.  
  33. { 
  34.  
  35.     protected static $maxAddressCount = 5; // 收获地址创建最大数量。 
  36.  
  37.    
  38.  
  39.     protected static function getMaxAddressCount() 
  40.  
  41.     { 
  42.  
  43.         return self::$maxAddressCount; 
  44.  
  45.     } 
  46.  
  47.    
  48.  
  49.     public function test() 
  50.  
  51.     { 
  52.  
  53.         echo self::getMaxAddressCount(); 
  54.  
  55.     } 
  56.  
  57. } 
  58.  
  59. $person = new Person(); 
  60.  
  61. $person->test(); 
  62.  
  63. // self 用法 3 :创建一个当前对象 
  64.  
  65.  
  66. // 单例示例 
  67.  
  68. class Person 
  69.  
  70. { 
  71.  
  72.     private static $instance = null; 
  73.  
  74.    
  75.  
  76.     private function __construct() {} 
  77.  
  78.    
  79.  
  80.     final public static function getInstance() 
  81.  
  82.     { 
  83.  
  84.         if (self::$instance == null) { 
  85.  
  86.             self::$instance = new self; 
  87.  
  88.         } 
  89.  
  90.         return self::$instance; 
  91.  
  92.     } 
  93.  
  94.    
  95.  
  96.     public function test() 
  97.  
  98.     { 
  99.  
  100.         echo "hello world!"; 
  101.  
  102.     } 
  103.  
  104. } 
  105.  
  106.    
  107.  
  108. $person = Person::getInstance(); 
  109.  
  110. $person->test(); 

关于 static 关键词的常见用法也在上面 3 个示例中得到综合体现

我深信上面的用法,任何一个入门的 PHPer 都是非常熟悉的,现在我要讲的是以下两种方式:

new self() 与 new static() 的区别?

我相信很多人都知道new self()创建一个当前类的对象,并不知道new static()也能创建一个当前类的对象。

关于new static()这种用法呢,在官方文档有说明,地址:https://www.php.net/manual/zh/language.oop5.late-static-bindings.php

PHP 官方把这种方式称为:后期静态绑定。

官方示例 1:

  1. class A { 
  2.  
  3.     public static function who() { 
  4.  
  5.         echo __CLASS__; 
  6.  
  7.     } 
  8.  
  9.     public static function test() { 
  10.  
  11.         self::who(); 
  12.  
  13.     } 
  14.  
  15. } 
  16.  
  17.    
  18.  
  19. class B extends A { 
  20.  
  21.     public static function who() { 
  22.  
  23.         echo __CLASS__; 
  24.  
  25.     } 
  26.  
  27. } 
  28.  
  29.    
  30.  
  31. B::test(); 

因为 Class B 继承了 Class A。 A 与 B 都有一个静态方法who(),此时通过B::test()的时候,调用的实际上是 Class A 的who()方法。

因为子类 Class B 的静态方法who()属于在 Class A 之后的子类里面才定义的,而 PHP 的默认特性只允许调用最先定义的。

就这引出了后期静态绑定的概念。

官方示例 2:

  1. class A { 
  2.  
  3.     public static function who() { 
  4.  
  5.         echo __CLASS__; 
  6.  
  7.     } 
  8.  
  9.     public static function test() { 
  10.  
  11.         static::who(); // 后期静态绑定从这里开始 
  12.  
  13.     } 
  14.  
  15. } 
  16.  
  17. class B extends A { 
  18.  
  19.     public static function who() { 
  20.  
  21.         echo __CLASS__; 
  22.  
  23.     } 
  24.  
  25. } 
  26.  
  27. B::test(); 

我们把 Class A 里面的test()方法体的self更改为static之后,static 代表的永远是指向调用类,也就是说虽然在 Class A 父类里面定义的方法与子类有同名冲突的情况,但是,当子类调用的时候,那么自动切换到子类的静态同名方法,取决于调用者。

大家可以通过运行以上两个示例进行理解。

Tags: Class self static

分享到: