PHP delayed static binding example sharing

高洛峰
Release: 2023-03-04 10:50:01
Original
1086 people have browsed it

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;;
Copy after login
// 输出结果
B
C
456789
abcdef
A
123456
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template