PHP靜態延遲綁定簡單示例
沒怎麼用過這個新特性,其實也不算新啦,試試吧,現在靜態類的繼承很方便了
class A {
protected static $def = '123456';
public static function test() {
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