3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting( -1); session_start(); interface Singleton_Interface { public static function getInstance(); } abstract class Singleton implements Singleton_Interface { public function __clone() { throw new Exception( 'Clone is not allowed.' ); } } class master extends Singleton implements Singleton_Interface { private $r; private static $instance; public static function getInstance( $params=array( ) ) { if( !isset(self::$instance) ) { $c = __CLASS__; self::$instance = new $c($params); } return self::$instance; } public function knock() { echo 'call ' . __METHOD__ . PHP_EOL; } public function __construct($r=false) { $this->r = $r; echo __METHOD__ . PHP_EOL; } public function __destruct() { echo __METHOD__ . PHP_EOL; if (method_exists($this->r, 'knock')) { $this->r->knock(); } $_SESSION['x'] = __METHOD__; } } class b extends Singleton implements Singleton_Interface { private $r; private static $instance; public static function getInstance( $params=array( ) ) { if( !isset(self::$instance) ) { $c = __CLASS__; self::$instance = new $c($params); } return self::$instance; } public function knock() { echo 'call ' . __METHOD__ . PHP_EOL; } public function __construct($r=false) { $this->r = $r; echo __METHOD__ . PHP_EOL; } public function __destruct() { echo __METHOD__ . PHP_EOL; if (method_exists($this->r, 'knock')) { $this->r->knock(); } $_SESSION['y'] = __METHOD__; } } header( 'content-type: text/plain' ); echo 'session:'; print_r($_SESSION); $master = master::getInstance(); $b = b::getInstance($master); // calls master->knock() on destruction $master->knock(); $b->knock(); echo 'done' . PHP_EOL;

preferences:
40.56 ms | 402 KiB | 5 Q