翼度科技»论坛 编程开发 PHP 查看内容

php Trait基类use trait,本类不use

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
一 回顾trait使用

https://blog.csdn.net/bushuwei/article/details/103514174
发现之前本人说明很模糊,自己居然不知道为什么
其实这里的$c,就是class B
再次回顾逻辑
二 分析



  • self和static区别说的没毛病
  • Trait基类use trait,本类不use。那么如果用的new self,则你new 出来的就是 use trait者。如果new static,则因为有继承关系, 它会判断类是否存在(父子会被认为都是同一个static,则不再new),那么,谁先调用instance,那么new出来的就是谁。‘Trait基类use trait,本类不use’->直接‘其实这里的$c,就是class B’是错的。之所以有这个‘幻觉’,是因为单例模式,且static
三 上代码


  • self+单例
    1. trait A{
    2.     private static $instance;
    3.     static function getInstance()
    4.     {
    5.         if(!isset(self::$instance)){
    6.             self::$instance = new self();
    7.         }
    8.         return self::$instance;
    9.     }
    10. }
    11. class B{
    12.     use A;
    13.     function a()
    14.     {
    15.         var_dump('call at B');
    16.     }
    17. }
    18. class C extends B{
    19.     function a()
    20.     {
    21.         var_dump('call at c');
    22.         parent::a();
    23.     }
    24. }
    25. class D extends B{
    26.     use A;
    27.     function a()
    28.     {
    29.         var_dump('call at D');
    30.         parent::a();
    31.     }
    32. }
    33. $b = B::getInstance();
    34. $c = C::getInstance();
    35. $d = D::getInstance();
    36. $c->a();
    37. echo "<br/>";
    38. $d->a();
    39. echo "<br/>";
    复制代码
      
    1. string(9) "call at B"
    2. string(9) "call at D" string(9) "call at B"
    复制代码
    注视掉
    1. // $b = B::getInstance();<br>结果不变
    复制代码
  • static+单例
    1. trait A{
    2.     private static $instance;
    3.     static function getInstance()
    4.     {
    5.         if(!isset(self::$instance)){
    6.             self::$instance = new static();
    7.         }
    8.         return self::$instance;
    9.     }
    10. }
    11. class B{
    12.     use A;
    13.     function a()
    14.     {
    15.         var_dump('call at B');
    16.     }
    17. }
    18. class C extends B{
    19.     function a()
    20.     {
    21.         var_dump('call at c');
    22.         parent::a();
    23.     }
    24. }
    25. class D extends B{
    26.     use A;
    27.     function a()
    28.     {
    29.         var_dump('call at D');
    30.         parent::a();
    31.     }
    32. }
    33. $b = B::getInstance();
    34. $c = C::getInstance();
    35. $d = D::getInstance();
    36. $c->a();
    37. echo "<br/>";
    38. $d->a();
    39. echo "<br/>";
    复制代码
    1. string(9) "call at B"
    2. string(9) "call at D" string(9) "call at B"
    复制代码
     
    注视掉
    1. // $b = B::getInstance();
    复制代码
    1. string(9) "call at c" string(9) "call at B"
    2. string(9) "call at D" string(9) "call at B"
    复制代码
     
    1. [/code] self+非单例
    2.   
    3. [code]trait A{
    4.     private static $instance;
    5.     static function getInstance()
    6.     {
    7.         self::$instance = new self();
    8.         return self::$instance;
    9.     }
    10. }
    11. class B{
    12.     use A;
    13.     function a()
    14.     {
    15.         var_dump('call at B');
    16.     }
    17. }
    18. class C extends B{
    19.     function a()
    20.     {
    21.         var_dump('call at c');
    22.         parent::a();
    23.     }
    24. }
    25. class D extends B{
    26.     use A;
    27.     function a()
    28.     {
    29.         var_dump('call at D');
    30.         parent::a();
    31.     }
    32. }
    33. $b = B::getInstance();
    34. $c = C::getInstance();
    35. $d = D::getInstance();
    36. $c->a();
    37. echo "<br/>";
    38. $d->a();
    39. echo "<br/>";
    复制代码
      
    1. string(9) "call at B"
    2. string(9) "call at D" string(9) "call at B"
    复制代码
      注释掉
    1. // $b = B::getInstance();
    复制代码
    结果不变
  •  static+非单例
    1. trait A{
    2.     private static $instance;
    3.     static function getInstance()
    4.     {
    5.         // if(!isset(self::$instance)){
    6.         //     self::$instance = new static();
    7.         // }
    8.         // return self::$instance;
    9.         self::$instance = new static();
    10.         return self::$instance;
    11.     }
    12. }
    13. class B{
    14.     use A;
    15.     function a()
    16.     {
    17.         var_dump('call at B');
    18.     }
    19. }
    20. class C extends B{
    21.     function a()
    22.     {
    23.         var_dump('call at c');
    24.         parent::a();
    25.     }
    26. }
    27. class D extends B{
    28.     use A;
    29.     function a()
    30.     {
    31.         var_dump('call at D');
    32.         parent::a();
    33.     }
    34. }
    35. $b = B::getInstance();
    36. $c = C::getInstance();
    37. $d = D::getInstance();
    38. $c->a();
    39. echo "<br/>";
    40. $d->a();
    41. echo "<br/>";
    复制代码
      
    1. string(9) "call at c" string(9) "call at B"
    2. string(9) "call at D" string(9) "call at B"
    复制代码
      注释掉
    $b = B::getInstance();
    结果不变


四 结论



  • self的话,就是谁use的,就是谁
  • static的话,谁调用的就是谁
  • 但是static会出现‘谁use就是谁’的幻觉,那是因为单例模式,前面创建了相同类型的单例。




来源:https://www.cnblogs.com/bushuwei/p/17277267.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具