3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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();
based on AJmhE
Output for 7.0.0 - 7.2.0
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.4.9 - 5.6.28
Fatal error: Call to private Foo::__construct() from invalid context in /in/jEPeD on line 38
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/jEPeD on line 3
Process exited with code 255.