3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Singleton { /** * @var Singleton[] The reference to *Singleton* instances of any child class. */ private static $instances = array(); /** * Returns the *Singleton* instance of this class. * * @return static The *Singleton* instance. */ public static function getInstance() { if (!isset(self::$instances[static::class])) { self::$instances[static::class] = new static(); } return self::$instances[static::class]; } /** * Protected constructor to prevent creating a new instance of the * *Singleton* via the `new` operator from outside of this class. */ protected function __construct() { } /** * Private clone method to prevent cloning of the instance of the * *Singleton* instance. * * @return void */ private function __clone() { } /** * Private unserialize method to prevent unserializing of the *Singleton* * instance. * * @return void */ private function __wakeup() { } } class SingletonChild extends Singleton { } class AnotherChild extends Singleton { } echo 'Expect: true, true, true, false' . PHP_EOL; var_dump(SingletonChild::getInstance() === SingletonChild::getInstance()); var_dump(AnotherChild::getInstance() === AnotherChild::getInstance()); $a = AnotherChild::getInstance(); var_dump($a === AnotherChild::getInstance()); var_dump($a === SingletonChild::getInstance());
Output for git.master, git.master_jit, rfc.property-hooks
Warning: The magic method Singleton::__wakeup() must have public visibility in /in/4TN3d on line 48 Expect: true, true, true, false bool(true) bool(true) bool(true) bool(false)

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:
157.93 ms | 405 KiB | 5 Q