zoukankan      html  css  js  c++  java
  • php中static和self的区别

    在阅读一些框架的源码时发现了new static(),和new self(),甚是不解,后来查阅资料,才知道static采用了延迟绑定,能准确知道是父类还是子类的调用。这就是说static是个聪明的小孩,家里的亲戚的辈分他都能准确的叫出;而不是像他的兄弟self,只知道自己的亲爹妈。

    例子如下:

    <?php
    
    class Father{
        protected  static $name = "father";
        public static function whoami_self(){
            echo self::$name."
    ";
        }
        
        public static function whoami_static(){
            echo static::$name."
    ";
        }
        
        public static function getInstance_self(){
            return new self();
        }
        
        public static function getInstance_static(){
            return new static();
        }
    }
    
    class FatherBrother extends Father{
        protected  static $name = "uncle";
    }
    
    FatherBrother::whoami_self(); //father
    FatherBrother::whoami_static(); //uncle
    var_dump(FatherBrother::getInstance_self()); //father
    var_dump(var_dump(FatherBrother::getInstance_static())); //uncle
    

     

    转载至:https://www.cnblogs.com/xdao/p/static_self_in_php.html

  • 相关阅读:
    ASP Loading
    haproxy中两个常用的宏
    数字签名-摘要等
    haproxy内存管理-free_list原理
    haproxy-代码阅读-内存管理
    网卡中断不均衡处理
    TIME_WAIT 另一种解决方式 SO_LINGER
    HTTP报文格式
    TIME_WAIT过多及解决
    awk如何向shell传值
  • 原文地址:https://www.cnblogs.com/starfish29/p/11217138.html
Copyright © 2011-2022 走看看