3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Container main class. * * @author Fabien Potencier */ class Container implements \ArrayAccess { private $values = array(); private $factories; private $protected; private $frozen = array(); private $raw = array(); private $keys = array(); /** * Instantiate the container. * * Objects and parameters can be passed as argument to the constructor. * * @param array $values The parameters or objects. */ public function __construct(array $values = array()) { $this->factories = new \SplObjectStorage(); $this->protected = new \SplObjectStorage(); foreach ($values as $key => $value) { $this->offsetSet($key, $value); } } /** * Sets a parameter or an object. * * Objects must be defined as Closures. * * Allowing any PHP callable leads to difficult to debug problems * as function names (strings) are callable (creating a function with * the same name as an existing parameter would break your container). * * @param string $id The unique identifier for the parameter or object * @param mixed $value The value of the parameter or a closure to define an object * @throws \RuntimeException Prevent override of a frozen service */ public function offsetSet($id, $value) { if (isset($this->frozen[$id])) { throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id)); } $this->values[$id] = $value; $this->keys[$id] = true; } /** * Gets a parameter or an object. * * @param string $id The unique identifier for the parameter or object * * @return mixed The value of the parameter or an object * * @throws \InvalidArgumentException if the identifier is not defined */ public function offsetGet($id) { if (!isset($this->keys[$id])) { throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } if ( isset($this->raw[$id]) || !is_object($this->values[$id]) || isset($this->protected[$this->values[$id]]) || !method_exists($this->values[$id], '__invoke') ) { var_dump('values'); return $this->values[$id]; } if (isset($this->factories[$this->values[$id]])) { var_dump('factories'); return $this->values[$id]($this); } var_dump('else'); $this->frozen[$id] = true; $this->raw[$id] = $this->values[$id]; return $this->values[$id] = $this->values[$id]($this); } /** * Checks if a parameter or an object is set. * * @param string $id The unique identifier for the parameter or object * * @return bool */ public function offsetExists($id) { return isset($this->keys[$id]); } /** * Unsets a parameter or an object. * * @param string $id The unique identifier for the parameter or object */ public function offsetUnset($id) { if (isset($this->keys[$id])) { if (is_object($this->values[$id])) { unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]); } unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]); } } /** * Marks a callable as being a factory service. * * @param callable $callable A service definition to be used as a factory * * @return callable The passed callable * * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object */ public function factory($callable) { if (!is_object($callable) || !method_exists($callable, '__invoke')) { throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.'); } $this->factories->attach($callable); return $callable; } /** * Protects a callable from being interpreted as a service. * * This is useful when you want to store a callable as a parameter. * * @param callable $callable A callable to protect from being evaluated * * @return callable The passed callable * * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object */ public function protect($callable) { if (!is_object($callable) || !method_exists($callable, '__invoke')) { throw new \InvalidArgumentException('Callable is not a Closure or invokable object.'); } $this->protected->attach($callable); return $callable; } /** * Gets a parameter or the closure defining an object. * * @param string $id The unique identifier for the parameter or object * * @return mixed The value of the parameter or the closure defining an object * * @throws \InvalidArgumentException if the identifier is not defined */ public function raw($id) { if (!isset($this->keys[$id])) { throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } if (isset($this->raw[$id])) { return $this->raw[$id]; } return $this->values[$id]; } /** * Extends an object definition. * * Useful when you want to extend an existing object definition, * without necessarily loading that object. * * @param string $id The unique identifier for the object * @param callable $callable A service definition to extend the original * * @return callable The wrapped callable * * @throws \InvalidArgumentException if the identifier is not defined or not a service definition */ public function extend($id, $callable) { if (!isset($this->keys[$id])) { throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) { throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id)); } if (!is_object($callable) || !method_exists($callable, '__invoke')) { throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.'); } $factory = $this->values[$id]; $extended = function ($c) use ($callable, $factory) { return $callable($factory($c), $c); }; if (isset($this->factories[$factory])) { $this->factories->detach($factory); $this->factories->attach($extended); } return $this[$id] = $extended; } /** * Returns all defined value names. * * @return array An array of value names */ public function keys() { return array_keys($this->values); } /** * Registers a service provider. * * @param ServiceProviderInterface $provider A ServiceProviderInterface instance * @param array $values An array of values that customizes the provider * * @return static */ public function register(ServiceProviderInterface $provider, array $values = array()) { $provider->register($this); foreach ($values as $key => $value) { $this[$key] = $value; } return $this; } } $container = new Container(); $foo = new \StdClass(); $foo->bar = 'fiz'; $container['foo'] = $foo; var_dump($container['foo']);

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.00617.00
8.3.50.0100.00521.90
8.3.40.0070.01418.83
8.3.30.0110.00419.07
8.3.20.0120.00320.14
8.3.10.0040.00423.70
8.3.00.0060.00319.04
8.2.180.0070.01118.54
8.2.170.0070.01722.96
8.2.160.0170.00322.08
8.2.150.0030.00624.18
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0040.00419.95
8.2.110.0090.00022.11
8.2.100.0110.00018.03
8.2.90.0000.00819.42
8.2.80.0000.00817.97
8.2.70.0030.00917.75
8.2.60.0000.00818.16
8.2.50.0030.00618.07
8.2.40.0000.00818.47
8.2.30.0060.00318.44
8.2.20.0030.00617.88
8.2.10.0040.00718.20
8.2.00.0060.00317.90
8.1.280.0110.01125.92
8.1.270.0060.00323.70
8.1.260.0130.00326.35
8.1.250.0080.00028.09
8.1.240.0030.00623.82
8.1.230.0040.00719.22
8.1.220.0030.00517.79
8.1.210.0040.00418.77
8.1.200.0060.00317.48
8.1.190.0000.00817.35
8.1.180.0040.00418.10
8.1.170.0030.00618.51
8.1.160.0060.00322.07
8.1.150.0030.00618.84
8.1.140.0000.00817.55
8.1.130.0000.00817.88
8.1.120.0000.00717.57
8.1.110.0050.00217.49
8.1.100.0040.00417.56
8.1.90.0050.00317.43
8.1.80.0000.00817.46
8.1.70.0030.00317.43
8.1.60.0040.00417.63
8.1.50.0090.00017.59
8.1.40.0090.00317.59
8.1.30.0070.00417.76
8.1.20.0090.00017.64
8.1.10.0080.00017.65
8.1.00.0040.00417.45
8.0.300.0070.00018.77
8.0.290.0040.00417.30
8.0.280.0000.00718.54
8.0.270.0040.00417.27
8.0.260.0070.00017.34
8.0.250.0000.00716.96
8.0.240.0030.00317.05
8.0.230.0000.00717.06
8.0.220.0030.00316.85
8.0.210.0030.00316.89
8.0.200.0040.00417.11
8.0.190.0050.00317.11
8.0.180.0000.00816.89
8.0.170.0000.00817.01
8.0.160.0040.00417.10
8.0.150.0040.00416.84
8.0.140.0080.00016.88
8.0.130.0000.00713.42
8.0.120.0060.00316.95
8.0.110.0040.00416.97
8.0.100.0040.00417.04
8.0.90.0030.00716.78
8.0.80.0060.01016.95
8.0.70.0040.00416.84
8.0.60.0000.00716.84
8.0.50.0000.00817.02
8.0.30.0070.01217.14
8.0.20.0120.00817.41
8.0.10.0030.00617.14
8.0.00.0100.01016.95
7.4.330.0000.00615.15
7.4.320.0040.00416.68
7.4.300.0070.00016.73
7.4.290.0030.00316.60
7.4.280.0000.00816.58
7.4.270.0050.00316.64
7.4.260.0040.00416.61
7.4.250.0000.00716.48
7.4.240.0050.00216.65
7.4.230.0070.00016.68
7.4.220.0040.01816.80
7.4.210.0090.00616.75
7.4.200.0030.00416.76
7.4.190.0040.00416.74
7.4.160.0070.01016.66
7.4.150.0070.01117.40
7.4.140.0130.00517.86
7.4.130.0050.01316.64
7.4.120.0120.00616.55
7.4.110.0030.01416.69
7.4.100.0150.00316.58
7.4.90.0070.01116.60
7.4.80.0170.00719.39
7.4.70.0160.00616.78
7.4.60.0140.00416.59
7.4.50.0050.00016.63
7.4.40.0130.01016.40
7.4.30.0060.01016.39
7.4.10.0170.00316.47
7.4.00.0040.01015.63
7.3.330.0030.00313.41
7.3.320.0030.00313.27
7.3.310.0000.00716.34
7.3.300.0000.00716.49
7.3.290.0120.00616.43
7.3.280.0060.01016.40
7.3.270.0090.00917.40
7.3.260.0090.01416.70
7.3.250.0110.01016.52
7.3.240.0100.01416.61
7.3.230.0110.00616.64
7.3.210.0100.00716.39
7.3.200.0140.00319.39
7.3.190.0090.00916.73
7.3.180.0070.01016.49
7.3.170.0060.01216.48
7.3.160.0160.00316.53
7.3.130.0070.01016.37
7.3.120.0090.00715.69
7.3.110.0070.00716.41
7.3.100.0040.01116.19
7.3.90.0060.00316.49
7.3.80.0110.00416.56
7.3.70.0070.00716.39
7.3.60.0000.01316.45
7.3.50.0070.00716.57
7.3.40.0040.01116.43
7.3.30.0060.00316.34
7.3.20.0000.01018.17
7.3.10.0050.01017.43
7.3.00.0090.00417.11
7.2.330.0060.01216.74
7.2.320.0070.01416.59
7.2.310.0120.01216.73
7.2.300.0080.00816.84
7.2.290.0100.00716.77
7.2.260.0070.01116.76
7.2.250.0070.01416.79
7.2.240.0090.00616.72
7.2.230.0070.00716.68
7.2.220.0000.01316.41
7.2.210.0080.00416.68
7.2.200.0090.00316.58
7.2.190.0000.01016.66
7.2.180.0060.00616.53
7.2.170.0040.00716.70
7.2.160.0000.01316.61
7.2.150.0090.00918.36
7.2.140.0070.00718.46
7.2.130.0050.00817.41
7.2.120.0080.00617.43
7.2.110.0020.01117.45
7.2.100.0100.00517.38
7.2.90.0050.00717.52
7.2.80.0070.00817.18
7.2.70.0050.00617.43
7.2.60.0090.00517.35
7.2.50.0060.00617.45
7.2.40.0030.01017.21
7.2.30.0090.00617.47
7.2.20.0060.00617.32
7.2.10.0070.00817.44
7.2.00.0040.00917.47
7.1.330.0070.00717.45
7.1.320.0000.01317.18
7.1.310.0000.01417.37
7.1.300.0040.01117.19
7.1.290.0050.00517.28
7.1.280.0080.00017.45
7.1.270.0000.01017.40
7.1.260.0140.00317.23
7.1.250.0020.00816.24
7.1.240.0060.00616.64
7.1.230.0070.01016.53
7.1.220.0050.01116.55
7.1.210.0060.00616.56
7.1.200.0050.00616.18
7.1.190.0020.01316.51
7.1.180.0010.00916.38
7.1.170.0050.00716.57
7.1.160.0050.00916.59
7.1.150.0020.00916.55
7.1.140.0020.01116.46
7.1.130.0080.00816.57
7.1.120.0070.00616.35
7.1.110.0090.00716.60
7.1.100.0050.00817.01
7.1.90.0060.00616.40
7.1.80.0080.00516.51
7.1.70.0040.00716.62
7.1.60.0080.01017.47
7.1.50.0030.01216.70
7.1.40.0070.00716.58
7.1.30.0050.00616.60
7.1.20.0070.00816.59
7.1.10.0050.00816.55
7.1.00.0060.02918.45
7.0.330.0070.00516.20
7.0.320.0050.00816.23
7.0.310.0040.01116.10
7.0.300.0040.01016.14
7.0.290.0060.00516.23
7.0.280.0070.00716.27
7.0.270.0060.00616.21
7.0.260.0050.00716.14
7.0.250.0030.00816.18
7.0.240.0100.00616.18
7.0.230.0050.00716.27
7.0.220.0050.00716.34
7.0.210.0010.01116.10
7.0.200.0030.01016.24
7.0.190.0070.00716.20
7.0.180.0080.00316.23
7.0.170.0030.00816.30
7.0.160.0060.00516.26
7.0.150.0050.00816.23
7.0.140.0030.03218.14
7.0.130.0060.01016.21
7.0.120.0070.00516.35
7.0.110.0090.00716.31
7.0.100.0070.02717.51
7.0.90.0100.03017.54
7.0.80.0080.03017.36
7.0.70.0070.02017.50
7.0.60.0130.02917.46
7.0.50.0070.03317.50
7.0.40.0030.02816.12
7.0.30.0040.03616.05
7.0.20.0060.02816.08
7.0.10.0050.03016.13
7.0.00.0070.02816.25
5.6.400.0120.00616.15
5.6.390.0120.00315.98
5.6.380.0060.00815.01
5.6.370.0090.00515.34
5.6.360.0080.00615.20
5.6.350.0050.01015.10
5.6.340.0030.01115.32
5.6.330.0070.00715.35
5.6.320.0020.01015.26
5.6.310.0060.00915.29
5.6.300.0050.00715.17
5.6.290.0100.00515.16
5.6.280.0060.02717.10
5.6.270.0020.01215.34
5.6.260.0050.01015.27
5.6.250.0070.02216.95
5.6.240.0100.02517.06
5.6.230.0060.02316.96
5.6.220.0070.03216.92
5.6.210.0050.03516.97
5.6.200.0060.03017.21
5.6.190.0070.03417.30
5.6.180.0080.03317.07
5.6.170.0060.02517.22
5.6.160.0060.02617.11
5.6.150.0040.03017.22
5.6.140.0070.02217.29
5.6.130.0070.02917.13
5.6.120.0090.03117.21
5.6.110.0040.03617.25
5.6.100.0050.03417.11
5.6.90.0070.02817.27
5.6.80.0050.02616.92
5.6.70.0070.02916.97
5.6.60.0020.02217.03
5.6.50.0030.02317.08
5.6.40.0050.03516.90
5.6.30.0090.02516.88
5.6.20.0090.03016.96
5.6.10.0100.02016.96
5.6.00.0080.02716.99
5.5.380.0040.02515.84
5.5.370.0030.02215.81
5.5.360.0020.02416.00
5.5.350.0020.02315.88
5.5.340.0050.01916.01
5.5.330.0070.02616.11
5.5.320.0060.03216.06
5.5.310.0030.02216.02
5.5.300.0050.02316.00
5.5.290.0060.02915.89
5.5.280.0050.03016.06
5.5.270.0080.03216.12
5.5.260.0060.01915.88
5.5.250.0070.03315.84
5.5.240.0070.01915.72
5.5.230.0030.03115.84
5.5.220.0070.02215.81
5.5.210.0050.01815.86
5.5.200.0070.02815.72
5.5.190.0040.03115.83
5.5.180.0080.02415.67
5.5.170.0080.00613.60
5.5.160.0060.03015.67
5.5.150.0060.02315.74
5.5.140.0050.03215.84
5.5.130.0060.03215.86
5.5.120.0070.02815.74
5.5.110.0070.03115.82
5.5.100.0030.03515.77
5.5.90.0030.03415.84
5.5.80.0110.02615.83
5.5.70.0100.02815.78
5.5.60.0060.01815.76
5.5.50.0040.01915.60
5.5.40.0060.03015.75
5.5.30.0010.02915.79
5.5.20.0050.03515.71
5.5.10.0080.03215.76
5.5.00.0020.03315.74
5.4.450.0100.01614.40
5.4.440.0040.02114.41
5.4.430.0070.02914.39
5.4.420.0060.02914.43
5.4.410.0050.03014.34
5.4.400.0040.01614.24
5.4.390.0060.02814.40
5.4.380.0090.02314.24
5.4.370.0080.02514.25
5.4.360.0080.02314.21
5.4.350.0090.02614.28
5.4.340.0060.02914.29
5.4.330.0060.00411.78
5.4.320.0070.02814.26
5.4.310.0060.03514.25
5.4.300.0070.02214.17
5.4.290.0120.02514.38
5.4.280.0100.02614.36
5.4.270.0060.03114.30
5.4.260.0100.01814.36
5.4.250.0060.03314.31
5.4.240.0050.03014.31
5.4.230.0050.02214.27
5.4.220.0040.03214.29
5.4.210.0110.01914.43
5.4.200.0020.02014.33
5.4.190.0070.02814.38
5.4.180.0060.01614.25
5.4.170.0060.02214.41
5.4.160.0060.02014.25
5.4.150.0050.03214.32
5.4.140.0070.01713.36
5.4.130.0080.02713.34
5.4.120.0050.01913.51
5.4.110.0040.03213.45
5.4.100.0080.02213.53
5.4.90.0070.02613.35
5.4.80.0040.03213.46
5.4.70.0090.02213.52
5.4.60.0070.02413.49
5.4.50.0100.02413.39
5.4.40.0050.02213.37
5.4.30.0060.02813.30
5.4.20.0030.02813.38
5.4.10.0080.02713.35
5.4.00.0070.02413.25
5.3.290.0030.03012.83
5.3.280.0070.04312.63
5.3.270.0020.04112.72
5.3.260.0070.04012.63
5.3.250.0100.02812.77
5.3.240.0050.03912.70
5.3.230.0070.03212.72
5.3.220.0070.03712.61
5.3.210.0020.03812.63
5.3.200.0040.03812.66
5.3.190.0090.03212.62
5.3.180.0060.02912.64
5.3.170.0070.04212.77
5.3.160.0100.02012.71
5.3.150.0070.03712.81
5.3.140.0070.02312.66
5.3.130.0070.03212.60
5.3.120.0090.03812.62
5.3.110.0090.04012.76
5.3.100.0120.03512.41
5.3.90.0070.03712.39
5.3.80.0040.02612.42
5.3.70.0070.03812.39
5.3.60.0050.04212.49
5.3.50.0030.04412.26
5.3.40.0050.04012.35
5.3.30.0050.03612.36
5.3.20.0050.02012.20
5.3.10.0060.02211.92
5.3.00.0050.04212.15

preferences:
37 ms | 401 KiB | 5 Q