后期静态绑定:static:: 不再被解析为定义当前方法或属性所在的类,而是在实际运行时计算的
可用 get_called_class() 函数来得到被调用的方法所在的类名
this是指向当前对象
self::指向当前的类 self就是写在哪个类里面, 实际调用的就是这个类
parent::指向父类
static指向执行类 static代表使用的这个类, 就是你在父类里写的static,然后被子类覆盖,使用的就是子类的方法或属性
class Animal {
public static $name = 'This is a animal';
public function __construct()
{
echo get_called_class()."<br>";
self::check();
static::check();
}
public static function what_animal() {
echo get_called_class()."<br>";
echo self::$name."<br>";
echo static::$name."<br>";
}
public static function check() {
echo '我是动物类'."<br>";
}
}
class Dog extends Animal {
public static $name = 'This is a dog';
public function __construct()
{
parent::__construct();
}
public static function check() {
echo '我是狗类';
}
}
Dog::what_animal();
/*
Dog
This is a animal
This is a dog
*/
$dog = new Dog();
/*
Dog
我是动物类
我是狗类
*/