VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php Trait基类use trait,本类不use

一 回顾trait使用

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

Top

二 分析

  • 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
Top

三 上代码

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

      

    string(9) "call at B"
    string(9) "call at D" string(9) "call at B" 

    注视掉

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

     

    注视掉

    1
    // $b = B::getInstance();
    string(9) "call at c" string(9) "call at B"
    string(9) "call at D" string(9) "call at B" 

     

  3. 1
     

     self+非单例

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    trait A{
        private static $instance;
        static function getInstance()
        {
            self::$instance = new self();
            return self::$instance;
        }
    }
     
    class B{
        use A;
        function a()
        {
            var_dump('call at B');
        }
    }
     
    class C extends B{
        function a()
        {
            var_dump('call at c');
            parent::a();
        }
    }
     
    class D extends B{
        use A;
        function a()
        {
            var_dump('call at D');
            parent::a();
        }
    }
    $b = B::getInstance();
    $c = C::getInstance();
    $d = D::getInstance();
     
    $c->a();
    echo "<br/>";
    $d->a();
    echo "<br/>";

      

    1
    2
    string(9) "call at B"
    string(9) "call at D" string(9) "call at B"

      注释掉

    1
    // $b = B::getInstance();

    结果不变

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

      

    1
    2
    string(9) "call at c" string(9) "call at B"
    string(9) "call at D" string(9) "call at B"

      注释掉
    $b = B::getInstance();
    结果不变
     

Top

四 结论

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

出处:https://www.cnblogs.com/bushuwei/p/17277267.html

相关教程