Home >Backend Development >PHP Tutorial >PHP delayed static binding example sharing

PHP delayed static binding example sharing

高洛峰
高洛峰Original
2016-12-26 16:18:351111browse

I haven’t used this new feature much, but it’s actually not new. Give it a try, inheritance of static classes is very convenient now

<?php
class A {
 protected static $def = &#39;123456&#39;;
 
 public static function test() {
  echo get_class(new static);
 }
 
 public static function test2() {
  echo static::$def;
 }
}
 
class B extends A {
 protected static $def = &#39;456789&#39;;
}
 
class C extends A {
 protected static $def = &#39;abcdef&#39;;
}
 
echo B::test();
echo &#39;<br>&#39;;
echo C::test();
echo &#39;<br>&#39;;
echo B::test2();
echo &#39;<br>&#39;;
echo C::test2();
echo &#39;<br>&#39;;
echo A::test();
echo &#39;<br>&#39;;
echo A::test2();
echo &#39;<br>&#39;;
// 输出结果
B
C
456789
abcdef
A
123456

For more PHP delayed static binding examples to share related articles, please follow PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn