3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 get_class(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;
based on rWEBr
Output for 7.2.0
A Warning: get_class() expects parameter 1 to be object, null given in /in/qZZWP on line 25 A A A A
Output for 5.3.18 - 7.1.10
A Warning: get_class() called without object from outside a class in /in/qZZWP on line 25 A A A A