3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Request { public $answer = 42; public function __construct() { echo 'Request created', PHP_EOL; } } class DI { protected $shared = []; protected $definitions = []; public function __construct() {} public function set($name, $definition, $shared = false) { if($shared) { if(isset($shared[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->shared[$name] = $definition; } else { if(isset($definitions[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->definitions[$name] = $definition; } } public function getRaw($name) { if(!isset($this->definitions[$name])) { throw new Exception("Definition '$name' is not set. Use DI::set('$name', <definition>) to set it."); } return $this->definitions[$name]; } public function get($name) { $definition = $this->getRaw($name); if(is_callable($definition)) return $definition(); if(class_exists($definition)) return new $definition(); } public function getShared($name) { if(!isset($this->shared[$name])) { throw new Exception("Shared definition '$name' is not set. Use DI::set('$name', <definition>, true) to set it."); } $shared = $this->shared[$name]; if(is_callable($shared)) $this->shared[$name] = $shared(); else if(is_string($shared) && class_exists($shared)) $this->shared[$name] = new $shared(); return $this->shared[$name]; } } $di = new DI(); //Using an anonymous function $di->set('request1', function(){ return new Request(); }); //Using an anonymous function $di->set('request2', 'Request', true); $request = $di->getShared('request2'); $request->answer = 5; var_dump($di->getShared('request2')); $request = $di->get('request1'); $request->answer = 5; var_dump($di->get('request1'));

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.0040.01118.43
8.3.50.0120.00721.15
8.3.40.0150.00318.84
8.3.30.0070.00719.09
8.3.20.0000.00720.38
8.3.10.0080.00021.77
8.3.00.0000.00819.26
8.2.180.0110.00416.32
8.2.170.0120.00322.96
8.2.160.0130.00320.21
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0030.00726.16
8.2.120.0040.00422.29
8.2.110.0030.00622.09
8.2.100.0070.00417.84
8.2.90.0040.00419.39
8.2.80.0030.00517.97
8.2.70.0030.00517.63
8.2.60.0040.00417.93
8.2.50.0060.00318.07
8.2.40.0030.00620.01
8.2.30.0000.00718.17
8.2.20.0050.00317.71
8.2.10.0040.00418.24
8.2.00.0040.00417.92
8.1.280.0140.00425.92
8.1.270.0000.00822.27
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0090.00023.96
8.1.230.0080.00419.16
8.1.220.0040.00417.74
8.1.210.0080.00018.77
8.1.200.0030.00717.48
8.1.190.0040.00417.48
8.1.180.0060.00318.10
8.1.170.0050.00318.62
8.1.160.0040.00421.96
8.1.150.0040.00418.85
8.1.140.0000.00717.43
8.1.130.0050.00217.90
8.1.120.0040.00417.41
8.1.110.0080.00017.50
8.1.100.0000.00817.50
8.1.90.0050.00217.51
8.1.80.0070.00017.55
8.1.70.0000.00817.39
8.1.60.0060.00317.67
8.1.50.0000.00817.46
8.1.40.0000.00817.59
8.1.30.0040.00417.74
8.1.20.0060.00317.61
8.1.10.0050.00217.61
8.1.00.0030.00617.53
8.0.300.0040.00418.77
8.0.290.0040.00416.75
8.0.280.0000.00818.48
8.0.270.0000.00817.19
8.0.260.0040.00417.38
8.0.250.0030.00316.90
8.0.240.0030.00317.03
8.0.230.0000.00716.98
8.0.220.0000.00716.93
8.0.210.0030.00316.86
8.0.200.0030.00316.89
8.0.190.0040.00417.07
8.0.180.0000.00716.95
8.0.170.0070.00016.93
8.0.160.0050.00317.04
8.0.150.0040.00416.91
8.0.140.0050.00216.97
8.0.130.0000.00613.38
8.0.120.0040.00416.91
8.0.110.0040.00416.87
8.0.100.0040.00417.05
8.0.90.0050.00316.91
8.0.80.0130.00316.96
8.0.70.0050.00216.89
8.0.60.0000.00716.82
8.0.50.0020.00516.74
8.0.30.0140.00617.16
8.0.20.0150.00817.40
8.0.10.0000.00817.07
8.0.00.0050.01216.90
7.4.330.0000.00515.12
7.4.320.0030.00316.57
7.4.300.0030.00316.57
7.4.290.0000.00716.52
7.4.280.0040.00416.69
7.4.270.0040.00416.67
7.4.260.0040.00416.64
7.4.250.0040.00416.41
7.4.240.0050.00216.66
7.4.230.0000.00716.38
7.4.220.0100.01016.62
7.4.210.0000.01516.63
7.4.200.0080.00016.68
7.4.160.0090.00616.63
7.4.150.0050.01217.40
7.4.140.0120.00817.86
7.4.130.0050.01216.69
7.4.120.0100.00816.68
7.4.110.0130.01016.60
7.4.100.0070.01616.57
7.4.90.0120.00416.57
7.4.80.0070.01119.39
7.4.70.0140.00316.64
7.4.60.0060.01216.60
7.4.50.0060.00316.55
7.4.40.0130.00616.47
7.4.30.0100.00816.45
7.4.00.0120.00615.03
7.3.330.0000.00513.45
7.3.320.0000.00513.45
7.3.310.0000.00716.45
7.3.300.0070.00016.28
7.3.290.0070.00816.36
7.3.280.0090.00816.45
7.3.270.0120.00617.40
7.3.260.0130.00616.50
7.3.250.0090.01216.52
7.3.240.0150.00916.49
7.3.230.0060.01316.61
7.3.210.0130.00316.61
7.3.200.0120.00619.39
7.3.190.0160.00616.42
7.3.180.0090.00716.62
7.3.170.0040.01516.61
7.3.160.0000.01616.50
7.3.10.0080.00416.63
7.3.00.0030.01316.54
7.2.330.0060.01216.73
7.2.320.0150.00316.65
7.2.310.0090.01016.79
7.2.300.0140.00316.65
7.2.290.0130.00616.83
7.2.130.0100.00317.13
7.2.120.0110.00416.88
7.2.110.0030.01116.93
7.2.100.0070.00316.98
7.2.90.0060.01017.13
7.2.80.0040.00716.85
7.2.70.0070.00317.16
7.2.60.0020.01316.95
7.2.50.0030.00617.01
7.2.40.0040.00416.79
7.2.30.0110.00416.97
7.2.20.0120.00317.19
7.2.10.0000.01217.12
7.2.00.0110.00718.29
7.1.250.0000.00915.61
7.1.200.0070.00415.95
7.1.100.0030.00618.00
7.1.70.0020.00517.28
7.1.60.0000.01217.27
7.1.50.0100.01416.75
7.1.00.0030.07722.40
7.0.200.0030.01416.89
7.0.140.0030.07721.96
7.0.60.0200.07020.05
7.0.50.0070.07717.94
7.0.40.0030.07320.06
7.0.30.0130.04320.24
7.0.20.0330.04720.23
7.0.10.0130.04020.07
7.0.00.0000.09020.22
5.6.210.0030.08720.72
5.6.200.0070.04018.18
5.6.190.0000.04720.63
5.6.180.0430.07020.41
5.6.170.0230.05720.55
5.6.160.0070.05020.54
5.6.150.0030.07718.19
5.6.140.0100.04318.25
5.6.130.0130.08318.19
5.6.120.0130.03320.91
5.6.110.0230.07020.91
5.6.100.0070.08020.99
5.6.90.0130.06721.13
5.6.80.0100.05020.43
5.5.350.0270.07720.39
5.5.340.0070.06718.05
5.5.330.0130.07720.27
5.5.320.0030.08720.27
5.5.310.0130.05020.35
5.5.300.0100.04018.09
5.5.290.0000.07018.04
5.5.280.0000.04320.98
5.5.270.0000.07320.78
5.5.260.0200.07320.66
5.5.250.0100.08020.68
5.5.240.0200.07020.18
5.4.450.4370.04019.64
5.4.440.3830.03719.27
5.4.430.3870.03719.54
5.4.420.4430.03719.56
5.4.410.3970.04019.29
5.4.400.4170.03718.89
5.4.390.3400.03318.88
5.4.380.3530.03719.08
5.4.370.3630.04019.20
5.4.360.3470.04018.83
5.4.350.3570.03318.86
5.4.340.3500.04319.22
5.4.320.3300.04019.22
5.4.310.4270.04319.00
5.4.300.3870.03719.21
5.4.290.4230.04019.15
5.4.280.3770.03319.15
5.4.270.3770.04019.06
5.4.260.3430.03718.85
5.4.250.3800.03319.15
5.4.240.4100.04019.14
5.4.230.4470.03318.94
5.4.220.4270.04019.07
5.4.210.4070.03319.13
5.4.200.2970.03719.14
5.4.190.4070.03019.13
5.4.180.4230.04719.30
5.4.170.3270.03719.07
5.4.160.4500.03019.13
5.4.150.4630.04019.18
5.4.140.4500.03316.39
5.4.130.4500.03016.39
5.4.120.4370.03716.43
5.4.110.3970.04016.50
5.4.100.3870.04316.50
5.4.90.3830.03716.31
5.4.80.3600.03716.30
5.4.70.3800.02716.62
5.4.60.3600.03716.43
5.4.50.4400.03316.48
5.4.40.4270.04016.51
5.4.30.4000.03716.46
5.4.20.4370.03316.48
5.4.10.4600.03016.43
5.4.00.4430.03715.83
5.3.290.4070.03714.71
5.3.280.3700.04314.71
5.3.270.3730.04014.68
5.3.260.4300.04014.83
5.3.250.4230.03314.65
5.3.240.4330.04314.66
5.3.230.4070.03714.57
5.3.220.4030.03714.47
5.3.210.4230.03714.65
5.3.200.3670.04014.61
5.3.190.3670.03714.47
5.3.180.3470.03714.56
5.3.170.3830.03014.65
5.3.160.3630.03314.57
5.3.150.4570.03714.54
5.3.140.3800.03014.56
5.3.130.3930.03714.52
5.3.120.4470.03014.45
5.3.110.4470.03714.71
5.3.100.4430.03713.96
5.3.90.3730.03713.99
5.3.80.3730.04013.94
5.3.70.4270.04014.10
5.3.60.4670.03313.98
5.3.50.4500.03714.06
5.3.40.4400.03314.02
5.3.30.3500.03013.91
5.3.20.3470.03713.73
5.3.10.2170.04313.56
5.3.00.0000.07013.55
5.2.170.3600.03011.20
5.2.160.3000.03011.06
5.2.150.2930.03011.38
5.2.140.3400.03311.23
5.2.130.3400.02311.15
5.2.120.3130.02711.27
5.2.110.0030.04711.05
5.2.100.0070.05711.03
5.2.90.2330.03711.01
5.2.80.4170.02010.99
5.2.70.2600.03011.10
5.2.60.3230.02711.14
5.2.50.2830.03310.95
5.2.40.2270.03011.15
5.2.30.2330.03311.09
5.2.20.2670.02710.88
5.2.10.2570.02710.83
5.2.00.2630.02010.93
5.1.60.2870.02310.12
5.1.50.2930.03010.00
5.1.40.3030.02310.20
5.1.30.2830.03310.26
5.1.20.3030.03310.57
5.1.10.3400.02710.00
5.1.00.3130.03010.18
5.0.50.1770.0208.70
5.0.40.0900.0178.30
5.0.30.1100.0278.30
5.0.20.0030.0178.10
5.0.10.0000.0208.07
5.0.00.0030.0308.26
4.4.90.1270.0135.86
4.4.80.1300.0135.86
4.4.70.1230.0175.83
4.4.60.1200.0105.84
4.4.50.1430.0175.81
4.4.40.1730.0235.84
4.4.30.1570.0135.88
4.4.20.1670.0205.87
4.4.10.1600.0205.91
4.4.00.1630.0275.87
4.3.110.1170.0135.87
4.3.100.0570.0175.87
4.3.90.0000.0135.88
4.3.80.0000.0235.88
4.3.70.0000.0135.88
4.3.60.0000.0135.88
4.3.50.0000.0135.88
4.3.40.0000.0235.88
4.3.30.0030.0135.88
4.3.20.0000.0135.88
4.3.10.0400.0235.88
4.3.00.0570.0175.89

preferences:
32.97 ms | 401 KiB | 5 Q