3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Enum { private $name; private static $enums; private function __construct($name) { $this->name = $name; } /** * Returns an assoc. array of ['ENUM_NAME' => $ENUM_VALUE] for all enum values. * @return array */ public static function getAll(): array { $class = static::class; if (!isset(self::$enums[$class])) static::init(); return self::$enums[$class]; } /** * Return an enum value (object) from a string name. * @return $this */ public static function fromString($name) { return static::__callStatic($name, []); } public function __toString() { return $this->name; } public static function __callStatic($name, $args) { $class = static::class; if (!isset(self::$enums[$class])) static::init(); if (!isset(self::$enums[$class][$name])) { throw new \TypeError('Undefined enum ' . $class . '::' . $name . '()'); } return self::$enums[$class][$name]; } private static function init() { $class = static::class; if ($class === __CLASS__) { throw new \Exception('Do not invoke methods directly on class Enum.'); } $doc = (new \ReflectionClass($class))->getDocComment(); if (preg_match_all('/@method\s+static\s+(\w+)/i', $doc, $matches)) { foreach ($matches[1] as $name) { self::$enums[$class][$name] = new static($name); } } else { throw new \Exception('Please provide a PHPDoc for ' . $class . ' with a static @method for each enum value.'); } } } /** * @method static RED() * @method static GREEN() * @method static BLUE() */ class Color extends Enum {} $red = Color::RED(); $red2 = Color::RED(); $green = Color::GREEN(); var_dump($red === $red2); var_dump($red == $red2); var_dump($red === $green); var_dump($red == $green); var_dump((string) $red); var_dump((string) $green); var_dump($red === Color::fromString('RED')); var_dump(Color::getAll()); try { $geern = Color::GEERN(); // Intentional typo. Throws. } catch (\Throwable $e) { echo get_class($e) . ': ' . $e->getMessage(). "\n"; } try { Enum::getAll(); // Shouldn't call methods directly on Enum. Throws. } catch (\Throwable $e) { echo get_class($e) . ': ' . $e->getMessage(). "\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.70.0130.00318.33
8.3.60.0090.00617.00
8.3.50.0120.00918.38
8.3.40.0000.01519.31
8.3.30.0110.00419.08
8.3.20.0150.00021.91
8.3.10.0040.00423.39
8.3.00.0040.00419.79
8.2.180.0090.01218.39
8.2.170.0110.00722.96
8.2.160.0110.00419.58
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0040.00422.20
8.2.120.0040.00426.35
8.2.110.0060.00321.06
8.2.100.0030.00918.16
8.2.90.0040.00419.48
8.2.80.0030.00618.00
8.2.70.0060.00317.88
8.2.60.0060.00318.05
8.2.50.0060.00318.07
8.2.40.0000.00818.35
8.2.30.0050.00318.28
8.2.20.0000.00818.31
8.2.10.0000.00818.36
8.2.00.0000.00818.30
8.1.280.0150.00325.92
8.1.270.0000.00820.70
8.1.260.0000.00826.35
8.1.250.0000.00728.09
8.1.240.0030.00622.75
8.1.230.0040.00719.13
8.1.220.0050.00317.77
8.1.210.0080.00018.77
8.1.200.0070.00317.50
8.1.190.0030.00517.72
8.1.180.0030.00518.10
8.1.170.0040.00419.08
8.1.160.0000.00818.98
8.1.150.0020.00518.93
8.1.140.0050.00319.70
8.1.130.0040.00417.66
8.1.120.0040.00417.69
8.1.110.0040.00417.60
8.1.100.0080.00017.71
8.1.90.0030.00617.57
8.1.80.0040.00417.58
8.1.70.0040.00417.57
8.1.60.0030.00517.87
8.1.50.0030.00617.61
8.1.40.0000.00917.64
8.1.30.0030.00517.94
8.1.20.0000.00817.88
8.1.10.0060.00317.78
8.1.00.0060.00317.57
8.0.300.0060.00318.90
8.0.290.0060.00317.00
8.0.280.0000.00718.43
8.0.270.0000.00817.25
8.0.260.0030.00317.02
8.0.250.0040.00417.22
8.0.240.0000.00817.21
8.0.230.0030.00317.15
8.0.220.0040.00417.08
8.0.210.0030.00317.04
8.0.200.0000.00717.20
8.0.190.0070.00417.16
8.0.180.0030.00517.23
8.0.170.0040.00417.20
8.0.160.0070.00317.18
8.0.150.0020.00517.14
8.0.140.0050.00217.16
8.0.130.0000.00613.67
8.0.120.0040.00416.95
8.0.110.0040.00417.15
8.0.100.0050.00317.02
8.0.90.0070.00017.11
8.0.80.0180.00317.08
8.0.70.0050.00317.01
8.0.60.0050.00217.11
8.0.50.0080.00017.22
8.0.30.0130.00617.23
8.0.20.0120.00917.40
8.0.10.0000.00817.33
8.0.00.0110.01317.11
7.4.330.0000.00515.55
7.4.320.0060.00016.80
7.4.300.0070.00016.88
7.4.290.0000.00716.89
7.4.280.0030.00616.71
7.4.270.0070.00016.77
7.4.260.0000.00816.79
7.4.250.0050.00216.70
7.4.240.0030.00316.86
7.4.230.0080.00016.96
7.4.220.0130.00616.78
7.4.210.0130.00716.66
7.4.200.0000.00716.63
7.4.160.0110.00616.83
7.4.150.0130.00617.40
7.4.140.0150.00417.86
7.4.130.0120.00916.77
7.4.120.0140.00816.74
7.4.110.0090.00916.78
7.4.100.0110.00716.67
7.4.90.0000.01916.72
7.4.80.0130.00919.39
7.4.70.0100.00716.63
7.4.60.0090.00616.70
7.4.50.0040.01216.84
7.4.40.0080.00816.82
7.4.30.0150.00316.68
7.4.00.0090.00615.14
7.3.330.0000.00613.71
7.3.320.0070.00013.56
7.3.310.0040.00416.47
7.3.300.0030.00316.66
7.3.290.0070.00916.62
7.3.280.0050.00916.62
7.3.270.0140.00417.40
7.3.260.0070.01117.02
7.3.250.0110.01016.81
7.3.240.0060.01316.74
7.3.230.0130.00716.81
7.3.210.0070.01516.57
7.3.200.0100.01016.68
7.3.190.0110.00716.72
7.3.180.0080.00816.78
7.3.170.0140.00316.78
7.3.160.0060.00916.61
7.3.70.0110.00815.14
7.3.60.0080.00515.21
7.3.50.0030.01615.00
7.3.40.0150.00015.11
7.3.30.0130.00915.13
7.3.20.0120.00915.08
7.3.10.0390.00416.20
7.3.00.0090.00716.31
7.2.330.0140.00316.84
7.2.320.0100.00716.71
7.2.310.0060.01616.75
7.2.300.0110.00716.71
7.2.290.0140.00416.89
7.2.200.0030.01615.35
7.2.190.0120.00615.34
7.2.180.0070.01015.31
7.2.170.0030.01115.21
7.2.160.0060.00615.22
7.2.150.0180.00615.36
7.2.140.0350.00315.50
7.2.130.0100.00916.40
7.2.120.0130.00616.23
7.2.110.0110.00716.41
7.2.100.0100.00716.20
7.2.90.0070.00916.52
7.2.80.0090.01216.58
7.2.70.0080.01016.46
7.2.60.0120.00516.55
7.2.50.0130.00516.32
7.2.40.0130.00816.33
7.2.30.0170.00616.56
7.2.20.0100.01116.28
7.2.10.0130.00616.52
7.2.00.0090.00817.32
7.1.300.0150.00614.24
7.1.290.0150.00314.18
7.1.280.0110.01914.27
7.1.270.0100.01314.34
7.1.260.0120.01214.31
7.1.250.0070.01115.23
7.1.240.0260.00614.32
7.1.230.0170.01114.03
7.1.220.0140.01414.05
7.1.210.0210.00414.22
7.1.200.0120.00815.17
7.1.190.0220.00314.19
7.1.180.0070.01514.23
7.1.170.0150.01114.23
7.1.160.0230.00514.28
7.1.150.0180.00714.24
7.1.140.0210.00414.13
7.1.130.0210.00314.05
7.1.120.0190.00614.19
7.1.110.0110.01114.38
7.1.100.0190.01014.33
7.1.90.0270.00314.36
7.1.80.0190.00814.06
7.1.70.0130.00615.72
7.1.60.0120.01015.95
7.1.50.0090.01115.69
7.1.40.0060.01315.48
7.1.30.0090.00915.67
7.1.20.0110.01015.55
7.1.10.0050.01115.51
7.1.00.0130.00615.71
7.0.330.0190.00813.87
7.0.320.0220.00613.82
7.0.310.0140.01413.87
7.0.300.0200.00713.82
7.0.290.0180.00713.89
7.0.280.0560.00913.84
7.0.270.0260.00313.67
7.0.260.0390.00313.93
7.0.250.0170.00913.81
7.0.240.0160.00814.05
7.0.230.0260.00413.79
7.0.220.0220.00313.75
7.0.210.0130.01613.97
7.0.200.0130.00515.33
7.0.190.0100.00815.40
7.0.180.0050.01015.14
7.0.170.0110.00915.20
7.0.160.0110.00615.19
7.0.150.0600.00415.06
7.0.140.0120.00415.27
7.0.130.0110.00715.49
7.0.120.0110.00615.33
7.0.110.0070.01015.10
7.0.100.0090.00915.21
7.0.90.0120.00715.36
7.0.80.0130.00415.09
7.0.70.0080.01015.20
7.0.60.0130.00515.16
7.0.50.0090.00815.22
7.0.40.0080.00714.50
7.0.30.0090.00814.43
7.0.20.0070.01214.52
7.0.10.0390.00314.56
7.0.00.0090.00714.57
5.6.400.0090.01213.63
5.6.390.0070.01813.82
5.6.380.0170.00314.07

preferences:
63.96 ms | 401 KiB | 5 Q