欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP延迟静态绑定示例分享

程序员文章站 2023-11-30 20:52:28
没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了

没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了

<?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>';

// 输出结果
b
c
456789
abcdef
a
123456