PHP static::、self::、new self()、new static()

PHP static::、self::、new self()、new static()

<?php
class Father{
    protected static $name = "大头";
    public static function father_self(){
        echo self::$name."\n";
    }
     
    public static function father_static(){
        echo static::$name."\n";
    }
     
    public static function getInstance_self(){
        return new self();
    }
     
    public static function getInstance_static(){
        return new static();
    }
}
 
class Son extends Father{
    protected  static $name = "小头";
}
 
Son::father_self(); //大头
echo "<br>";
Son::father_static(); //小头
echo "<br>";
var_dump(Son::getInstance_self()); //Father
echo "<br>";
var_dump(Son::getInstance_static()); //Son
posted @ 2021-05-18 08:55  JingDR  阅读(63)  评论(0)    收藏  举报