3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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;
Output for 5.2.3 - 7.1.0
session:Array ( ) master::__construct b::__construct call master::knock call b::knock done master::__destruct b::__destruct call master::knock
Output for 5.0.2 - 5.2.2
Warning: session_start(): Cannot send session cookie - headers already sent in /in/0jbkt on line 4 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /in/0jbkt:4) in /in/0jbkt on line 4 session:Array ( ) master::__construct b::__construct call master::knock call b::knock done master::__destruct b::__destruct call master::knock
Output for 5.0.0 - 5.0.1
Warning: session_start(): Cannot send session cookie - headers already sent in /in/0jbkt on line 4 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /in/0jbkt:4) in /in/0jbkt on line 4 session:Array ( ) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 43 master::__constructPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 79 b::__constructPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 37 call master::knockPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 73 call b::knockPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 106 donePHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 48 master::__destructPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 84 b::__destructPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/0jbkt on line 37 call master::knockPHP_EOL
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING in /in/0jbkt on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING in /in/0jbkt on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/0jbkt on line 6
Process exited with code 255.