PHP Delayed Static Binding Sample Sharing
- 2021-07-02 23:44:54
- OfStack
Haven't used this new feature very much, in fact, it's not new, try it, now the inheritance of static classes is very convenient
<?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 '<br>';
echo C::test();
echo '<br>';
echo B::test2();
echo '<br>';
echo C::test2();
echo '<br>';
echo A::test();
echo '<br>';
echo A::test2();
echo '<br>';
// Output result
B
C
456789
abcdef
A
123456