3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { public static $instance; public static function getInstance() { if (self::$instance === null) { self::$instance = new static(); } return static::$instance; } } class B extends A { public static $instance; } class C extends A { } echo get_class(A::getInstance()) . PHP_EOL; echo var_dump(B::getInstance()) . PHP_EOL; //As you can see, a new B is instantiated here, because B::$instance is a priority over A::$instance. echo get_class(A::getInstance()) . PHP_EOL; A::$instance = null; echo PHP_EOL; echo get_class(A::getInstance()) . PHP_EOL; echo get_class(C::getInstance()) . PHP_EOL; //But here, there is no C::$instance, so let's check the A::$instance which is not empty. echo get_class(A::getInstance()) . PHP_EOL;
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
A NULL A A A A

preferences:
149.91 ms | 404 KiB | 227 Q