3v4l.org

run code in 300+ PHP versions simultaneously
<?php class FooSingleton extends BarSingleton { private $foo_instance; public static function getInstance() { if (!self::$foo_instance) { self::$foo_instance = new self; } } } class BarSingleton { private $bar_instance; public static function getInstance() { if (!self::$bar_instance) { // Defer to FooSingleton self::$bar_instance = parent::getInstance(); } } } $foo = FooSingleton::getFooInstance(); $bar = BarSingleton::getBarInstance(); // This should be true, but what if we want to override both but we're unaware that they are both separate singletons var_dump($foo === $bar);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Fatal error: Cannot use "parent" when current class scope has no parent in /in/aOUpn on line 27
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: Cannot use "parent" when current class scope has no parent in /in/aOUpn on line 27 Fatal error: Uncaught Error: Call to undefined method FooSingleton::getFooInstance() in /in/aOUpn:34 Stack trace: #0 {main} thrown in /in/aOUpn on line 34
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Fatal error: Uncaught Error: Call to undefined method FooSingleton::getFooInstance() in /in/aOUpn:34 Stack trace: #0 {main} thrown in /in/aOUpn on line 34
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Call to undefined method FooSingleton::getFooInstance() in /in/aOUpn on line 34
Process exited with code 255.

preferences:
150.51 ms | 402 KiB | 214 Q