首页 > 编程语言 >php Trait基类use trait,本类不use

php Trait基类use trait,本类不use

时间:2023-03-31 19:23:54浏览次数:51  
标签:use getInstance Trait self instance call static 本类

一 回顾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就是谁’的幻觉,那是因为单例模式,前面创建了相同类型的单例。



标签:use,getInstance,Trait,self,instance,call,static,本类
From: https://www.cnblogs.com/bushuwei/p/17277267.html

相关文章

  • clickhouse创建2个表
    oracle的建表语句: --UPCENTER.STK_RISE_DROP_INFOdefinitionCREATETABLE"UPCENTER"."STK_RISE_DROP_INFO"("ISVALID"NUMBER(1,0)DEFAULT1NOTNULLENABLE,"CREATETIME"TIMESTAMP(6)DEFAULTSYSDATENOTN......
  • 解决videojs 在Chrome浏览器下报:A network error caused the media download to fail
    记录一下videoJS在Chrome浏览器下有时候出现播放一半或者回退的一个恶心bug,错误提示如下:Anetworkerrorcausedthemediadownloadtofailpart-way.经过一下午的折腾查找,终于在GitHub上看到他们官方的一个解决方案,这个方案目前没有更新在官方文档最新版本中,只是随便提了一下......
  • ansible 常用模块 user
    模块说明管理用户帐户和用户属性。此模块用于useradd创建、usermod修改和userdel删除帐户。参数ParameterCommentsappend boolean如果true,将用户添加到中指定的组groups。如果false,用户将只被添加到在指定的组中groups,将他们从所有其他组中删......
  • use macOS terminal can not install MicroPython on Raspberry Pi Pico bug All In O
    usemacOSterminalcannotinstallMicroPythononRaspberryPiPicobugAllInOne使用macOS终端无法在RaspberryPiPico上安装MicroPythonbugMicroPython环境搭建您可以通过USB将Pico连接到计算机,然后将文件拖放到它上面来对Pico进行编程,因此我们整理了......
  • react useComparedState
    import{useCallback,useRef,useState}from'react';import{shallowEqual}from"../utils/shallowEqual";functionuseComparedState(initialState:any){const[state,setState]=useState(initialState);conststateRef=useRef......
  • 37、K8S-安全机制-CA、User、Cluster
    1、证书信息1.1、简介我们知道,通过kubeadm在创建集群的时候,其中有一步就是:生成kubernetes控制组件的kubeconfig文件及相关的启动配置文件,通过各种conf文件,让不同的组件具备操作相关资源的权限。1.2、位置master1~]#ll/etc/kubernetes/pki/-rw-r--r--1rootroot131......
  • SDL_PauseAudio 声音播放/暂停函数剖析
    函数说明用此函数来暂停播放音频,或播放音频。根据参数来决定,0是播放音频,非0是暂停播放音频切记,并不是停止播放音频!函数声明/***\namePauseaudiofunctions**T......
  • Expression #1 of ORDER BY clause is not in SELECT list
    mysql问题Code:3065,SQLState:HY000,Expression#1ofORDERBYclauseisnotinSELECTlist,referencescolumn'name'whichisnotinSELECTlist;thisisincom......
  • rust对T和&T实现trait意义
    如果T实现了某个trait,那么整个T类型的&T,也实现了该trait了吗?不是这样的。为什么这样,例子说明下:traitSpeaker{fnspeak(&self);}structBasicSpeaker;implSpe......
  • THFuse: An infrared and visible image fusion network using transformer and hybri
    THFuse:Aninfraredandvisibleimagefusionnetworkusingtransformerandhybridfeatureextractor一种基于Transformer和混合特征提取器的红外与可见光图像融合网......