3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Singleton { private $storage; // A Temporary variable for 'quick data' protected $methods = array(); // Anonymous Function Declaration protected static $getInstance; // Instance of the Container public static function __callStatic($methodName, $arguments = array()) { return self::getInstance()->Skeleton( $methodName, $arguments ); } public static function newInstance() { // Start a new instance of the class and pass any arguments $class = new \ReflectionClass( get_called_class() ); self::$getInstance = $class->newInstanceArgs( func_get_args() ); return self::$getInstance; } public static function getInstance() { // see if the class has already been called this run if (!empty(self::$getInstance)) return self::$getInstance; $calledClass = get_called_class(); // check if the object has been sterilized in the session // This will invoke the __wake up operator if (isset($_SESSION) && array_key_exists( $calledClass, $_SESSION ) && is_object( self::$getInstance = unserialize( $_SESSION[$calledClass] ))) return self::$getInstance; // Start a new instance of the class and pass any arguments $class = new \ReflectionClass( get_called_class() ); self::$getInstance = $class->newInstanceArgs( func_get_args() ); return self::$getInstance; } /** * @param null $object * @return object|null */ public static function clearInstance($object = null) { self::$getInstance = is_object( $object ) ? $object : null; if (array_key_exists( __CLASS__, $_SESSION )) unset($_SESSION[__CLASS__]); return self::$getInstance; } public function __call($methodName, $arguments = array()) { return $this->Skeleton( $methodName, $arguments ); } private function Skeleton($methodName, $arguments = array()) { // Have we used addMethod() to override an existing method if (key_exists( $methodName, $this->methods )) return (null === ($result = call_user_func_array( $this->methods[$methodName], $arguments )) ? $this : $result); // Is the method in the current scope ( public, protected, private ). // Note declaring the method as private is the only way to ensure single instancing if (method_exists( $this, $methodName )) { return (null === ($result = call_user_func_array( array($this, $methodName), $arguments )) ? $this : $result); } if (key_exists( 'closures', $GLOBALS ) && key_exists( $methodName, $GLOBALS['closures'] )) { $function = $GLOBALS['closures'][$methodName]; $this->addMethod( $methodName, $function ); return (null === ($result = call_user_func_array( $this->methods[$methodName], $arguments )) ? $this : $result); } throw new \Exception( "There is valid method or closure with the given name '$methodName' to call" ); } private function addMethod($name, $closure) { if (is_callable( $closure )): $this->methods[$name] = \Closure::bind( $closure, $this, get_called_class() ); else: // Nested to ensure Singleton returns the correct value of self throw new \Exception( "New Method Must Be A Valid Closure" ); endif; } public function __wakeup() { if (method_exists( $this, '__construct' )) self::__construct(); $object = get_object_vars( $this ); foreach ($object as $item => $value) // TODO - were really going to try and objectify everything? if(is_object( $temp = @unserialize($this->$item))) $this->$item = $temp; } // for auto class serialization add: const Singleton = true; to calling class public function __sleep() { if (!defined( 'self::Singleton' ) || !self::Singleton) return null; $object = get_object_vars( $this ); foreach ($object as $key => &$value) { if (empty($value) || empty($this->$key)) continue; // The object could be null from serialization? if (is_object( $value )) { try { $this->$key = @serialize( $value ); } catch (\Exception $e){ continue; } // Database object we need to catch the error thrown. if ($_SESSION[__CLASS__][0] != 'O') continue; } $onlyKeys[] = $key; } return (isset($onlyKeys) ? $onlyKeys : null); } public function __destruct() { // We require a sleep function to be set manually for singleton to manage utilization if (!defined( 'self::Singleton' ) || !self::Singleton) return null; try { $_SESSION[__CLASS__] = @serialize( $this ); // I was using if (__sleep != null).. but resulted in errors, this is a shitty fix ill admit } catch (\Exception $e){ unset($_SESSION[__CLASS__]); return null; }; if ($_SESSION[__CLASS__][0] != 'O') unset($_SESSION[__CLASS__]); // We only want to store objects, if } // The rest of the methods are for the sake of methods public function &__get($variable) { return $GLOBALS[$variable]; } public function __set($variable, $value) { $GLOBALS[$variable] = $value; } public function __isset($variable) { return array_key_exists( $variable, $GLOBALS ); } public function __unset($variable) { unset($GLOBALS[$variable]); } public function __invoke() { return $this->storage; } private function set($name, $value = null) { $this->storage = null; if ($value == null) { if (is_array( $name )) $this->storage = $name; else $this->storage[] = $name; } else $this->storage[$name] = $value; return $this; } private function get($variable = null) { return ($variable == null ? $this->storage : $this->{$variable}); } private function has($variable) { return isset($this->$variable); } } class stdOJB implements \ArrayAccess { use Singleton; const Singleton = true; // turns on auto caching public function __construct() { return null; } public function offsetSet($offset, $value) { if (is_null($offset)) $this->storage[] = $value; else $this->storage[$offset] = $value; } public function offsetExists($offset) { return isset($this->storage[$offset]); } public function offsetUnset($offset) { unset($this->storage[$offset]); } public function offsetGet($offset) { return isset($this->storage[$offset]) ? $this->storage[$offset] : null; } public function &__get($variable) { return $this->storage[$variable]; } public function __set($variable, $value) { $this->storage[$variable] = $value; } public function __isset($variable) { return array_key_exists( $variable, $this->storage ); } public function __unset($variable) { unset($this->storage[$variable]); } public function __destruct() { return null; } } abstract class QuickFetch { protected $db; public function fetch_into_current_class($array) { $object = get_object_vars( $this ); foreach ($array as $key => $value) if (array_key_exists( $key, $object )) $this->$key = $value; } public function fetch_as_object($sql,... $execute) { try { $stmt = $this->db->prepare( $sql ); $stmt->setFetchMode( \PDO::FETCH_CLASS, Skeleton::class ); $stmt->execute( $execute ); $stmt = $stmt->fetchAll(); // user obj return (is_array( $stmt ) && count( $stmt ) == 1 ? array_pop( $stmt ) : $stmt); } catch (\Exception $e) { sortDump( $e ); } } } class user extends QuickFetch { use Singleton; const Singleton = true; public $info; private function getPublicData() { if (!empty($this->courseId) && (!is_object( $this->course ) || $this->course->course_id != $this->courseI)) $this->course = (object) ['course_id' => 3]; // $this->fetch_as_object( $sql, $id ); echo $this->course; exit(1); if (!empty($this->boxColor)) { if (!is_object( $this->course )) throw new \Error(); // $this->course->distance = $this->fetch_as_object( 'SELECT * FROM StatsCoach.golf_distance WHERE course_id = ? AND distance_color = ?', $this->courseId, $this->boxColor ); if (!isset($this->course->distance) || !is_object( $this->course->distance ) || $this->course->distance->distance_color != $this->boxColor) { $this->course->distance = $this->fetch_as_object( 'SELECT * FROM StatsCoach.golf_distance WHERE course_id = ? AND distance_color = ?', $this->courseId, $this->boxColor ); return null; } if (!is_object( $this->course->distance )) throw new \Exception(); return null; } } } user::getPublicData();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0120.00318.56
8.3.50.0060.00916.69
8.3.40.0070.00719.08
8.3.30.0000.01518.73
8.3.20.0040.00420.48
8.3.10.0030.00621.85
8.3.00.0000.00920.73
8.2.180.0070.00718.29
8.2.170.0090.01222.96
8.2.160.0140.00022.12
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0080.00020.66
8.2.120.0000.00826.35
8.2.110.0030.00620.68
8.2.100.0090.00318.16
8.2.90.0090.00019.33
8.2.80.0050.00518.05
8.2.70.0060.00317.88
8.2.60.0000.00818.05
8.2.50.0040.00418.16
8.2.40.0060.00820.77
8.2.30.0030.00521.10
8.2.20.0000.00818.11
8.2.10.0080.00018.20
8.2.00.0000.00918.23
8.1.280.0070.00725.92
8.1.270.0000.00923.67
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0060.00320.88
8.1.230.0000.01119.24
8.1.220.0000.00817.90
8.1.210.0030.00618.77
8.1.200.0070.00317.47
8.1.190.0030.00617.22
8.1.180.0080.00018.10
8.1.170.0030.00518.53
8.1.160.0000.00819.05
8.1.150.0000.00819.05
8.1.140.0040.00419.73
8.1.130.0040.00419.05
8.1.120.0000.00717.63
8.1.110.0030.00517.57
8.1.100.0030.00517.58
8.1.90.0080.00017.46
8.1.80.0050.00317.63
8.1.70.0050.00317.57
8.1.60.0030.00517.67
8.1.50.0000.00917.66
8.1.40.0030.00517.65
8.1.30.0000.00817.69
8.1.20.0030.00617.64
8.1.10.0060.00317.68
8.1.00.0040.00417.67
8.0.300.0030.00618.77
8.0.290.0000.00816.88
8.0.280.0000.00718.63
8.0.270.0040.00417.34
8.0.260.0030.00316.89
8.0.250.0000.00717.15
8.0.240.0000.00817.07
8.0.230.0040.00417.10
8.0.220.0050.00317.13
8.0.210.0070.00017.07
8.0.200.0000.00717.01
8.0.190.0030.00517.18
8.0.180.0050.00317.00
8.0.170.0050.00317.00
8.0.160.0030.00517.04
8.0.150.0000.00717.08
8.0.140.0040.00417.10
8.0.130.0070.00013.55
8.0.120.0080.00016.95
8.0.110.0040.00417.04
8.0.100.0040.00417.07
8.0.90.0040.00416.95
8.0.80.0160.00717.04
8.0.70.0040.00416.97
8.0.60.0040.00417.03
8.0.50.0040.00416.94
8.0.30.0090.01217.19
8.0.20.0080.01317.41
8.0.10.0040.00417.10
8.0.00.0070.01116.89
7.4.330.0030.00315.55
7.4.320.0050.00316.72
7.4.300.0040.00416.63
7.4.290.0010.00616.61
7.4.280.0060.00316.56
7.4.270.0040.00316.68
7.4.260.0040.00416.63
7.4.250.0000.00716.60
7.4.240.0040.00416.77
7.4.230.0040.00416.60
7.4.220.0110.00716.67
7.4.210.0060.01016.63
7.4.200.0030.00316.46
7.4.160.0080.00816.76
7.4.150.0140.00417.40
7.4.140.0150.00617.86
7.4.130.0030.01716.64
7.4.120.0100.01316.68
7.4.110.0080.01116.63
7.4.100.0100.01016.74
7.4.90.0090.00916.73
7.4.80.0130.01019.39
7.4.70.0060.01216.46
7.4.60.0100.00716.70
7.4.50.0040.00916.55
7.4.40.0150.00616.40
7.4.30.0070.01016.73
7.4.00.0030.01415.05
7.3.330.0050.00013.42
7.3.320.0000.00513.44
7.3.310.0070.00016.48
7.3.300.0000.00716.41
7.3.290.0000.00716.58
7.3.280.0080.01016.58
7.3.270.0070.01217.40
7.3.260.0100.00816.84
7.3.250.0110.01116.63
7.3.240.0070.01016.62
7.3.230.0140.01016.67
7.3.210.0100.00716.57
7.3.200.0160.00516.57
7.3.190.0060.01216.81
7.3.180.0090.00916.64
7.3.170.0040.01216.58
7.3.160.0100.00716.70
7.2.330.0160.01016.81
7.2.320.0180.00016.86
7.2.310.0070.01016.95
7.2.300.0110.00716.86
7.2.290.0110.00616.68
7.2.60.0090.00616.70
7.2.00.0040.01119.25
7.1.200.0080.00415.97
7.1.100.0060.00618.14
7.1.70.0040.00417.21
7.1.60.0700.01333.50
7.1.50.0670.01733.11
7.1.40.0600.01732.50
7.1.30.0630.01732.69
7.1.20.0930.00732.85
7.1.10.0500.01314.91
7.1.00.0470.01314.91
7.0.200.0730.01015.06
7.0.190.0500.01015.01
7.0.180.0600.00714.48
7.0.170.0630.01314.51
7.0.160.0770.00714.75
7.0.150.0570.01014.63
7.0.140.0500.01014.55
7.0.130.0500.01314.89
7.0.120.0600.01315.06
7.0.110.0500.01014.70
7.0.100.0500.01014.78
7.0.90.0600.01014.55
7.0.80.0530.00714.57
7.0.70.0630.00314.70
7.0.60.0600.00714.47
7.0.50.0670.01714.72
7.0.40.0470.01315.02
7.0.30.0530.00314.71
7.0.20.0500.00714.70
7.0.10.0470.01014.90
7.0.00.1130.01314.78

preferences:
49.73 ms | 400 KiB | 5 Q