php静态延迟绑定简单示例
没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了
class A {
protected static $def = '123456';
public static function test() {
立即学习“PHP免费学习笔记(深入)”;
echo get_class(new static);
}
public static function test2() {
echo static::$def;
}
}
class B extends A {
protected static $def = '456789';
}
class C extends A {
protected static $def = 'abcdef';
}
echo B::test();
echo '
';
echo C::test();
echo '
';
echo B::test2();
echo '
';
echo C::test2();
echo '
';
echo A::test();
echo '
';
echo A::test2();
echo '
';
// 输出结果
B
C
456789
abcdef
A
123456
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号