3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait singleton { /** * private construct, generally defined by using class */ //private function __construct() {} public static function getInstance() { static $_instance = NULL; $class = __CLASS__; return $_instance ?: $_instance = new $class; } public function __clone() { trigger_error('Cloning '.__CLASS__.' is not allowed.',E_USER_ERROR); } public function __wakeup() { trigger_error('Unserializing '.__CLASS__.' is not allowed.',E_USER_ERROR); } } /** * Example Usage */ class foo { use singleton; private function __construct() { $this->name = 'foo'; } } class bar { use singleton; private function __construct() { $this->name = 'bar'; } } $foo = foo::getInstance(); echo $foo->name; $bar = bar::getInstance(); echo $bar->name;
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Creation of dynamic property foo::$name is deprecated in /in/16GIC on line 32 foo Deprecated: Creation of dynamic property bar::$name is deprecated in /in/16GIC on line 40 bar

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
53.14 ms | 401 KiB | 8 Q