3v4l.org

run code in 300+ PHP versions simultaneously
<?php class OpenStruct extends ArrayObject { public function __construct($input = array()) { parent::__construct($input, static::ARRAY_AS_PROPS); } public function offsetSet($key, $value) { if (is_array($value)) { parent::offsetSet($key, new static($value)); } else { parent::offsetSet($key, $value); } } public function offsetGet($key) { $raw = parent::offsetGet($key); if (is_callable($raw)) { return call_user_func($raw); } return $raw; } public function __call($method, $args) { $raw = parent::offsetGet($method); if (is_callable($raw)) { if (version_compare(PHP_VERSION, '5.4.0', '>=') && $raw instanceof \Closure) { $raw->bindTo($this); } return call_user_func_array($raw, $args); } } static public function fromJson($json) { if (! is_string($json)) { throw new InvalidArgumentException('Argument must be a string.'); } $input = json_decode($json, true); if (null === $input) { throw new InvalidArgumentException('Argument must be a string containing valid JSON.'); } return new static($input); } } class Struct extends OpenStruct { public function __construct($input) { parent::__construct($input); } public function offsetSet($key, $value) { if (! $this->offsetExists($key)) { throw new RuntimeException(sprintf('Undefined field "%s"', $key)); } parent::offsetSet($key, $value); } public function offsetGet($key) { if (! $this->offsetExists($key)) { throw new RuntimeException(sprintf('Undefined field "%s"', $key)); } parent::offsetGet($key); } public function offsetUnset($key) { throw new RuntimeException(sprintf('Cannot unset field "%s"', $key)); } } // OpenStruct with Constructor Parameters // Cannot Change Structure $person = new OpenStruct(array('first' => 'Joe', 'last' => 'Bloggs', 'age' => 20)); // Nested Array becomes OpenStruct $person->address = array( 'address' => '123 Alphabet Street', 'city' => 'Awesome City', 'postcode' => 'ABC 123', 'country' => 'Awesome Country' ); $person->first; // $person['first']; $person->last; // $person['last']; $person->age; // $person['age']; $person->address->city; // $person['address']['city']; // Nested OpenStruct $person->hobbies = new OpenStruct(array('Football', 'Ice Hockey', 'Formula 1')); // Traversable foreach ($person->hobbies as $hobby) { echo 'Hobby: ' . $hobby . "\n"; } // Convert to JSON echo 'JSON: ' . $person->toJson() . "\n"; print_r($person); // OpenStruct with no Constructor $person = new OpenStruct; $person->name = 'Joe'; $person['name'] = 'John'; print_r($person); // Struct Requires Constructor Parameters // Cannot Change Structure try { $person = new Struct(array('first' => 'Joe', 'last' => 'Bloggs', 'hobbies' => array('Football', 'Ice Hockey', 'Formula 1'))); print_r($person); $person->age = 30; } catch (RuntimeException $e) { echo 'Cannot change Struct' . "\n"; } // OpenStruct from JSON String // Can Change Structure $json = OpenStruct::fromJson('{"first":"Joe","last":"Bloggs","age":20,"address":{"address":"123 Alphabet Street","city":"Awesome City","postcode":"ABC 123","country":"Canada"}}'); $json->foo = 'bar'; print_r($json); // Struct from JSON String // Cannot Change Structure try { $json = Struct::fromJson('{"first":"Joe","last":"Bloggs","age":20,"address":{"address":"123 Alphabet Street","city":"Awesome City","postcode":"ABC 123","country":"Canada"}}'); print_r($json); $json->foo = 'bar'; } catch (RuntimeException $e) { echo 'Cannot change Struct' . "\n"; } // OpenStructs with Closures $open = new OpenStruct; $open->first = 'Joe'; $open->last = 'Bloggs'; $open->fullName = function() use ($open) { return $open->first . ' ' . $open->last; }; /* // PHP 5.4 Style $open->fullName = function() { return $this->first . ' ' . $this->last; }; */ $open->last = 'Smith'; print_r($open->fullName()); // OpenStruct from Object $object = new StdClass; $object->first = 'Joe'; $object->last = 'Bloggs'; $open = new OpenStruct($object); $open->first; print_r($open); // Struct from Object $object = new StdClass; $object->first = 'Joe'; $object->last = 'Bloggs'; try { $open = new Struct($object); print_r($open); $open->age = 30; } catch (RuntimeException $e) { echo 'Cannot change Struct' . "\n"; }

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.0090.00616.61
8.3.50.0110.00616.55
8.3.40.0070.00718.88
8.3.30.0120.00318.89
8.3.20.0050.00318.95
8.3.10.0060.00318.95
8.3.00.0030.00719.26
8.2.180.0110.00718.29
8.2.170.0070.00722.96
8.2.160.0040.01419.04
8.2.150.0050.00324.18
8.2.140.0000.00924.66
8.2.130.0000.00826.16
8.2.120.0000.00821.18
8.2.110.0060.00321.04
8.2.100.0090.00317.83
8.2.90.0030.00517.63
8.2.80.0040.00417.97
8.2.70.0030.00617.62
8.2.60.0100.00017.93
8.2.50.0040.00418.07
8.2.40.0050.00519.47
8.2.30.0070.00417.87
8.2.20.0060.00317.75
8.2.10.0050.00317.69
8.2.00.0040.00417.82
8.1.280.0090.00625.92
8.1.270.0040.00423.99
8.1.260.0080.00026.35
8.1.250.0030.00628.09
8.1.240.0060.00617.61
8.1.230.0050.00517.72
8.1.220.0030.00517.74
8.1.210.0050.00318.77
8.1.200.0060.00317.48
8.1.190.0050.00317.35
8.1.180.0040.00418.10
8.1.170.0050.00319.03
8.1.160.0000.00819.14
8.1.150.0000.00818.76
8.1.140.0040.00417.58
8.1.130.0000.00717.45
8.1.120.0050.00317.55
8.1.110.0040.00417.58
8.1.100.0080.00017.44
8.1.90.0070.00017.49
8.1.80.0060.00317.53
8.1.70.0000.00817.55
8.1.60.0000.00817.58
8.1.50.0000.00817.59
8.1.40.0030.00517.59
8.1.30.0000.00817.74
8.1.20.0040.00417.74
8.1.10.0030.00517.55
8.1.00.0040.00417.52
8.0.300.0050.00318.77
8.0.290.0040.00416.88
8.0.280.0000.00818.68
8.0.270.0070.00017.33
8.0.260.0050.00216.96
8.0.250.0040.00417.12
8.0.240.0050.00217.09
8.0.230.0060.00317.06
8.0.220.0000.00717.03
8.0.210.0000.00716.98
8.0.200.0030.00316.96
8.0.190.0040.00417.03
8.0.180.0040.00717.07
8.0.170.0070.00417.10
8.0.160.0000.00817.01
8.0.150.0040.00416.86
8.0.140.0050.00317.05
8.0.130.0030.00313.45
8.0.120.0030.00616.83
8.0.110.0040.00416.89
8.0.100.0000.00716.86
8.0.90.0060.00317.02
8.0.80.0060.00916.98
8.0.70.0060.00317.07
8.0.60.0000.00816.98
8.0.50.0090.00016.91
8.0.30.0090.00917.07
8.0.20.0100.01017.11
8.0.10.0040.00417.12
8.0.00.0100.00816.77
7.4.330.0000.00512.98
7.4.320.0030.00316.66
7.4.300.0030.00316.73
7.4.290.0060.00316.67
7.4.280.0000.00816.69
7.4.270.0030.00316.61
7.4.260.0000.00613.34
7.4.250.0040.00416.64
7.4.240.0000.00716.72
7.4.230.0070.00016.67
7.4.220.0110.01116.68
7.4.210.0090.00616.63
7.4.200.0100.00016.42
7.4.190.0040.00416.86
7.4.160.0080.00816.75
7.4.150.0160.00916.56
7.4.140.0090.01417.86
7.4.130.0120.01316.59
7.4.120.0090.00916.60
7.4.110.0070.01116.65
7.4.100.0120.00816.61
7.4.90.0120.00616.56
7.4.80.0080.01119.39
7.4.70.0030.01616.68
7.4.60.0060.01316.65
7.4.50.0040.00416.62
7.4.40.0030.01316.59
7.4.30.0100.01716.61
7.4.00.0030.01314.88
7.3.330.0070.00013.51
7.3.320.0030.00313.48
7.3.310.0000.00716.54
7.3.300.0000.00716.51
7.3.290.0100.00816.51
7.3.280.0140.00016.42
7.3.270.0080.00916.53
7.3.260.0090.00916.76
7.3.240.0080.00916.74
7.3.230.0120.00616.54
7.3.210.0040.01316.46
7.3.200.0130.00416.60
7.3.190.0100.00716.54
7.3.180.0100.01016.52
7.3.170.0130.01016.71
7.3.160.0000.01716.64
7.3.120.0090.00614.98
7.2.330.0180.00016.95
7.2.320.0100.00816.85
7.2.310.0070.01016.68
7.2.300.0040.01216.75
7.2.290.0150.00316.94
7.2.00.0000.01619.52
7.1.100.0000.01218.00
7.1.70.0030.00617.30
7.1.60.0040.01117.18
7.1.50.0070.01516.79
7.1.00.0000.03722.30
7.0.200.0030.00616.86
7.0.140.0130.06721.95
7.0.120.0030.06721.96
7.0.100.0470.07020.06
7.0.90.0170.08020.05
7.0.80.0330.05319.99
7.0.70.0400.07020.09
7.0.60.0600.07020.15
7.0.50.0330.05720.43
7.0.40.0100.08020.01
7.0.30.0130.07320.07
7.0.20.0030.04720.09
7.0.10.0100.08020.02
7.0.00.0130.08020.07
5.6.280.0000.08020.91
5.6.250.0100.07720.75
5.6.240.0100.08020.69
5.6.230.0030.05320.69
5.6.220.0070.08020.72
5.6.210.0130.07020.51
5.6.200.0070.07721.16
5.6.190.0100.08321.17
5.6.180.0130.07021.19
5.6.170.0270.07321.02
5.6.160.0030.08721.11
5.6.150.0130.07021.10
5.6.140.0030.09021.13
5.6.130.0130.05721.16
5.6.120.0130.05021.19
5.6.110.0070.09021.00
5.6.100.0130.07021.05
5.6.90.0070.05321.11
5.6.80.0000.08720.53
5.6.70.0070.07720.46
5.6.60.0130.07720.43
5.6.50.0100.06320.61
5.6.40.0000.04320.38
5.6.30.0130.05720.41
5.6.20.0100.05320.48
5.6.10.0070.08020.34
5.6.00.0070.07020.48
5.5.380.0170.08020.52
5.5.370.0070.04020.61
5.5.360.0070.08320.46
5.5.350.0170.04020.48
5.5.340.0170.07320.83
5.5.330.0130.06720.96
5.5.320.0100.08020.96
5.5.310.0170.05020.93
5.5.300.0200.07320.93
5.5.290.0070.08020.95
5.5.280.0070.07020.82
5.5.270.0200.07720.88
5.5.260.0130.08020.93
5.5.250.0170.05720.76
5.5.240.0100.08020.30
5.5.230.0100.08020.21
5.5.220.0070.07720.27
5.5.210.0030.08020.36
5.5.200.0170.07320.25
5.5.190.0030.08320.33
5.5.180.0030.05020.24
5.5.160.0030.08320.29
5.5.150.0030.08720.19
5.5.140.0070.07020.18
5.5.130.0170.07020.24
5.5.120.0130.07320.12
5.5.110.0000.08320.29
5.5.100.0030.07320.14
5.5.90.0030.04020.13
5.5.80.0030.05020.22
5.5.70.0130.07320.00
5.5.60.0100.07320.11
5.5.50.0200.07020.06
5.5.40.0130.07020.16
5.5.30.0100.07720.15
5.5.20.0070.08020.06
5.5.10.0070.07720.10
5.5.00.0130.07720.15
5.4.450.0070.05019.37
5.4.440.0030.07319.57
5.4.430.0170.07019.27
5.4.420.0200.06019.53
5.4.410.0070.07719.07
5.4.400.0000.08018.95
5.4.390.0070.03719.03
5.4.380.0100.07719.14
5.4.370.0200.06719.05
5.4.360.0030.05719.09
5.4.350.0030.08719.05
5.4.340.0130.07019.19
5.4.320.0030.07719.05
5.4.310.0070.06018.91
5.4.300.0130.07019.05
5.4.290.0070.07719.20
5.4.280.0070.07719.00
5.4.270.0100.03319.13
5.4.260.0100.07719.03
5.4.250.0100.04319.04
5.4.240.0070.08318.84
5.4.230.0130.05319.20
5.4.220.0070.08019.02
5.4.210.0070.06319.22
5.4.200.0170.06019.12
5.4.190.0030.08319.13
5.4.180.0070.05319.22
5.4.170.0130.07319.02
5.4.160.0070.06019.16
5.4.150.0130.07719.19
5.4.140.0130.07016.54
5.4.130.0130.06016.32
5.4.120.0130.06316.43
5.4.110.0100.07016.50
5.4.100.0170.04016.39
5.4.90.0070.06716.52
5.4.80.0030.07716.41
5.4.70.0130.07016.51
5.4.60.0030.08016.47
5.4.50.0070.07716.42
5.4.40.0100.06716.50
5.4.30.0070.08016.29
5.4.20.0070.07016.39
5.4.10.0100.06716.45
5.4.00.0030.04015.90
5.3.290.0030.06714.66
5.3.280.0100.06014.73
5.3.270.0030.05014.70
5.3.260.0030.05014.65
5.3.250.0130.04014.61
5.3.240.0070.07314.74
5.3.230.0070.08314.64
5.3.220.0170.06714.71
5.3.210.0130.07014.66
5.3.200.0100.07314.64
5.3.190.0030.04714.64
5.3.180.0030.08314.63
5.3.170.0070.06314.62
5.3.160.0100.07314.78
5.3.150.0100.04014.66
5.3.140.0100.05714.70
5.3.130.0070.06314.65
5.3.120.0070.07314.69
5.3.110.0000.08014.69
5.3.100.0070.04714.25
5.3.90.0030.06714.02
5.3.80.0100.06714.00
5.3.70.0000.04714.08
5.3.60.0000.04013.99
5.3.50.0030.08014.09
5.3.40.0070.07014.09
5.3.30.0070.03713.91
5.3.20.0130.06313.81
5.3.10.0100.06313.74
5.3.00.0000.07713.85

preferences:
34.89 ms | 401 KiB | 5 Q