3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Singleton { private static $instance; private function __construct() {} private function __clone() {} private function __wakeup() {} public static function getInstance() { if (empty(static::$instanse)) { return new static(); } else { return static::$instance; } } } class Foo { use Singleton; private $bar = 2; public function incBar() { $this->bar++; } public function getBar() { return $this->bar."<br/>"; } } $foo = new Foo(); $foo = Foo::getInstance(); echo $foo->getBar(); $foo->incBar(); echo $foo->getBar();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: The magic method Singleton::__wakeup() must have public visibility in /in/jEPeD on line 9 Fatal error: Uncaught Error: Call to private Foo::__construct() from global scope in /in/jEPeD:38 Stack trace: #0 {main} thrown in /in/jEPeD on line 38
Process exited with code 255.
Output for 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
Fatal error: Uncaught Error: Call to private Foo::__construct() from invalid context in /in/jEPeD:38 Stack trace: #0 {main} thrown in /in/jEPeD on line 38
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Call to private Foo::__construct() from invalid context in /in/jEPeD on line 38
Process exited with code 255.

preferences:
153.01 ms | 402 KiB | 183 Q