摘要:<?php//后期静态绑定(延迟静态绑定)class Father //定义一个父类{ public static $money = 51000; //静态属性 public static function getClass() //静态方法 {
<?php
//后期静态绑定(延迟静态绑定)
class Father //定义一个父类
{
public static $money = 51000; //静态属性
public static function getClass() //静态方法
{
return __CLASS__; //返回当前类名
}
public static function getMoney()
{
return static::getClass() . '::' . static::$money;
}
}
//定义子类
class Son extends Father
{
public static function getClass() //覆写父类的静态方法
{
return __CLASS__; //返回当前类名
}
public static $money = 366666; // 覆写父类的静态属性
}
//调用父类中的静态方法,来获取类名
echo Father::getClass(),'<br>';
echo Father::getMoney(),'<br>';
//调用子类类中的静态方法,来获取类名
echo Son::getClass(),'<br>';
echo Son::$money,'<hr>';
echo Son::getMoney(),'<hr>';
批改老师:天蓬老师批改时间:2019-01-04 16:51:58
老师总结:静态后期绑定是个难点, 重点在于执行时再确定类成员的调用者, 也代码书写时可以不一致,这个要注意