php Trait基类use trait,本类不use

一 回顾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

三 上代码

  1. self+单例
    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" 

    注视掉

    // $b = B::getInstance();
    结果不变
  2. static+单例
    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/>";
    
    string(9) "call at B"
    string(9) "call at D" string(9) "call at B" 
    

     

    注视掉

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

     

  3. 
    

     self+非单例

      

    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/>";
    

      

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

      注释掉

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

    结果不变

  4.  static+非单例
    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/>";
    

      

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

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

四 结论

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