3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface IErrorManager { public function ariseFatal($message); public function ariseWarning($message); public function ariseNotice($message); } interface ILexer { public function tokenize($input); } interface IParser { public function setKeywords(array $keywords); public function parse($input); } interface IParseResult { public function getDefinition(); public function getName(); public function getConstants(); } interface INameValidator { public function validate($name); } interface ICompiler { public function compile($input); } class ErrorManager implements IErrorManager { public function ariseFatal($message) { $this->triggerError($message, E_USER_FATAL); } public function ariseWarning($message) { $this->triggerError($message, E_USER_WARNING); } public function ariseNotice($message) { $this->triggerError($message, E_USER_NOTICE); } private function triggerError($message, $type) { trigger_error((string)$message, (int)$type); } } abstract class CompilerElement { private $errorManager; protected function getErrorManager() { return $this->errorManager; } protected function __construct(IErrorManager $errorManager) { $this->errorManager = $errorManager; } } class Lexer extends CompilerElement implements ILexer { public function tokenize($input) { if (!is_string($input)) { $this->getErrorManager()->ariseFatal(get_class($this).'::parse expects parameter 1 to be string. '.gettype($input).' given.'); } } public function __construct(IErrorManager $errorManager) { parent::__construct($errorManager); } } class Parser extends CompilerElement implements IParser { private $lexer; private $nameValidator; private $keywords = array(); public function setKeywords(array $keywords) { $this->keywords = $keywords; } public function parse($input) { if (!is_string($input)) { $this->getErrorManager()->ariseFatal(get_class($this).'::parse expects parameter 1 to be string. '.gettype($input).' given.'); } $errorManager = $this->getErrorManager(); $input = $this->cleanUp($input); $tokens = $this->lexer->tokenize($input); $result = array(); $currentResult = null; $processingBody = false; reset($tokens); while (($token = key($tokens)) !== false && ($tokenValue = current($tokens)) !== false) { switch ($token) { case 'type' && $processingBody === false: if ($currrentResult !== null || !isset($this->keywords[$tokenValue])) { $errorManager->ariseFatal('Syntax error. Unexpected '.$tokenValue); } $currentResult = new ParseResult($this->keywords[$tokenValue], $errorManager); break; case 'name' && $processingBody === false: // Will this ever happen? if ($currentResult === null) { $errorManager->ariseFatal('Syntax error. Unexpected '.$tokenValue); } $this->nameValidator->validate($name); $currentResult->setName($name); break; case 'start_body': if ($currentResult === null || $currentResult->getName() === null) { $errorManager->ariseFatal('Syntax error. Unexpected '.$tokenValue); } $processingBody = true; break; case 'end_body': if ($currentResult === null || $currentResult->getName() === null || $processingBody === false) { $errorManager->ariseFatal('Syntax error. Unexpected '.$tokenValue); } $result[] = $currentResult; $currentResult = null; $processingBody = false; break; default: if ($processingBody === true && $currentResult !== null) { $value = $tokenValue !== null ? $tokenValie : 0; $currentResult->setItem($token, $value); } else { $errorManager->ariseFatal('Syntax error. Unexpected '.$tokenValue); } break; } next($tokens); } return $return; } private function cleanUp($input) { return trim($input); } public function __construct(ILexer $lexer, INameValidator $nameValidator, IErrorManager $errorManager) { parent::__construct($errorManager); $this->lexer = $lexer; $this->nameValidator = $nameValidator; } } class ParseResult extends CompilerElement implements IParseResult { private $definition; private $name; private $constants; public function getDefinition() { return $this->definition; } public function getName() { return $this->name; } public function getConstants() { return $this->constants; } public function setItem($name, $value) { if (!is_string($name)) { $this->getErrorManager()->ariseFatal('Enumeration item name can only be string.'); } if (!is_numeric($value)) { $this->getErrorManager()->ariseFatal('Enumeration item can only hold numeric value.'); } $this->constants[$name] = (ctype_digit($value) === false) ? (int)$value : (double)$value; } public function setName($name) { if (!is_string($name)) { $this->getErrorManager()->ariseFatal(get_class($this).'::setName expects parameter 1 to be string. '.gettype($name).' given.'); } $this->name = $name; } public function __construct($definition, IErrorManager $errorManager) { parent::__construct($errorManager); if (!is_string($definition)) { $this->getErrorManager()->ariseFatal(get_class($this).'::__construct expects parameter 1 to be string. '.gettype($definition).' given.'); } $this->definition = $definition; } } class NameValidator extends CompilerElement implements INameValidator { private $allowedChars; public function validate($name) { if (!is_string($name)) { $this->gerErrorManager()->ariseFatal(get_class($this).'::validate expects parameter 1 to be string. '.gettype($name).' given.'); } $nameLength = strlen($name); for ($i = 0; $i < $nameLength; $i++) { $current = $name[$i]; if ($i === 0 && !ctype_alpha($current)) { $this->gerErrorManager()->ariseFatal('Name should start with alphabetic characters. Given name '.$name.' starts with: '.$current); } if (!in_array($current, $this->allowedChars, true)) { $this->gerErrorManager()->ariseFatal('Unexpected character '.$current.' in name '.$name.'.'); } } } public function __construct(IErrorManager $errorManager) { parent::__construct($errorManager); // Yes, I hate that everything is allowed as name in php. $this->allowedChars = str_split('abcdefghijklmnopqrstuvwxyz0123456789'); } } class Enum2PhpCompiler implements ICompiler { private $parser; const INTENDATION_CHAR = "\t"; public function compile($input) { $parseResult = $this->parser->parse($input); $return = $parseResult->getDefinition().' '.$parseResult->getName().' {'.PHP_EOL; foreach ($parseResult->getConstants() as $name => $value) { $return .= Compiler::INTENDATION_CHAR.'const '.$name.' = '.$value.PHP_EOL; } $return .= Compiler::INTENDATION_CHAR.'private function __construct() {}'.PHP_EOL; $return .= '}'; return $return; } public function __construct(IParser $parser) { $this->parser = $parser; } } $code = <<<PHP enum MyEnum { Item1, Item2 = 5, Item3 } PHP;

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.0060.00916.63
8.3.50.0120.00822.12
8.3.40.0150.00419.02
8.3.30.0090.00619.04
8.3.20.0040.00420.43
8.3.10.0030.00523.66
8.3.00.0050.00320.89
8.2.180.0210.00318.42
8.2.170.0120.00422.96
8.2.160.0070.00722.26
8.2.150.0110.00424.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0040.01422.14
8.2.110.0030.00622.25
8.2.100.0110.00418.03
8.2.90.0040.00419.35
8.2.80.0030.00518.03
8.2.70.0040.00417.50
8.2.60.0080.00018.05
8.2.50.0030.00618.07
8.2.40.0100.00019.95
8.2.30.0030.00618.19
8.2.20.0080.00017.79
8.2.10.0050.00317.88
8.2.00.0000.00817.77
8.1.280.0040.01125.92
8.1.270.0060.00323.82
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0050.00523.96
8.1.230.0090.00319.18
8.1.220.0040.00417.79
8.1.210.0040.00418.77
8.1.200.0030.00717.25
8.1.190.0000.00817.63
8.1.180.0060.00318.10
8.1.170.0050.00318.63
8.1.160.0040.00422.03
8.1.150.0000.00818.86
8.1.140.0000.00717.58
8.1.130.0000.00717.96
8.1.120.0070.00317.46
8.1.110.0040.00417.49
8.1.100.0070.00017.61
8.1.90.0000.00717.45
8.1.80.0040.00417.53
8.1.70.0030.00517.54
8.1.60.0090.00017.61
8.1.50.0030.00517.65
8.1.40.0000.00817.54
8.1.30.0030.00617.77
8.1.20.0050.00317.84
8.1.10.0100.00217.56
8.1.00.0000.00817.53
8.0.300.0040.00418.77
8.0.290.0080.00017.28
8.0.280.0070.00018.45
8.0.270.0050.00317.41
8.0.260.0030.00317.03
8.0.250.0000.00717.14
8.0.240.0040.00417.10
8.0.230.0030.00317.05
8.0.220.0030.00317.02
8.0.210.0040.00416.98
8.0.200.0000.01017.14
8.0.190.0040.00417.15
8.0.180.0050.00217.02
8.0.170.0060.00317.02
8.0.160.0030.00617.18
8.0.150.0050.00316.93
8.0.140.0040.00417.04
8.0.130.0060.00013.53
8.0.120.0000.00817.09
8.0.110.0000.00817.13
8.0.100.0020.00517.04
8.0.90.0050.00316.90
8.0.80.0060.01217.02
8.0.70.0040.00416.90
8.0.60.0020.00516.95
8.0.50.0050.00316.87
8.0.30.0070.01317.14
8.0.20.0060.01317.40
8.0.10.0050.00317.23
8.0.00.0120.00716.78
7.4.330.0060.00015.05
7.4.320.0000.00716.75
7.4.300.0000.00816.66
7.4.290.0070.00016.64
7.4.280.0040.00416.49
7.4.270.0040.00416.70
7.4.260.0000.00716.66
7.4.250.0000.00816.71
7.4.240.0060.00116.66
7.4.230.0070.00016.85
7.4.220.0150.00616.77
7.4.210.0090.00516.66
7.4.200.0040.00416.72
7.4.190.0040.00416.91
7.4.160.0110.00616.41
7.4.150.0070.01117.40
7.4.140.0100.00917.86
7.4.130.0010.01616.69
7.4.120.0090.01216.66
7.4.110.0130.00716.44
7.4.100.0090.01216.57
7.4.90.0140.00716.68
7.4.80.0070.01416.61
7.4.70.0070.01116.59
7.4.60.0070.01016.50
7.4.50.0040.00416.64
7.4.40.0000.01422.77
7.4.30.0060.00916.55
7.4.10.0000.01814.91
7.4.00.0090.00915.06
7.3.330.0030.00313.32
7.3.320.0030.00313.41
7.3.310.0000.00716.39
7.3.300.0000.00716.43
7.3.290.0060.00916.45
7.3.280.0090.00916.45
7.3.270.0120.00617.40
7.3.260.0120.00916.73
7.3.250.0140.00616.53
7.3.240.0000.01716.44
7.3.230.0100.00716.40
7.3.210.0090.00916.45
7.3.200.0100.01319.39
7.3.190.0060.01016.47
7.3.180.0090.00916.46
7.3.170.0100.00716.63
7.3.160.0130.00916.55
7.3.130.0040.01414.52
7.3.120.0060.00914.79
7.3.110.0000.01914.73
7.3.100.0070.00414.98
7.3.90.0000.01414.97
7.3.80.0070.00714.99
7.3.70.0070.01114.58
7.3.60.0030.01014.49
7.3.50.0100.00314.81
7.3.40.0090.00614.73
7.3.30.0030.01014.82
7.3.20.0040.01116.68
7.3.10.0030.01016.64
7.3.00.0070.00716.24
7.2.330.0030.01316.21
7.2.320.0110.00716.60
7.2.310.0030.01316.23
7.2.300.0060.01616.41
7.2.290.0130.00316.52
7.2.260.0070.01015.06
7.2.250.0130.00315.09
7.2.240.0100.00314.70
7.2.230.0030.01014.69
7.2.220.0070.00715.01
7.2.210.0040.01414.37
7.2.200.0030.00614.69
7.2.190.0100.00714.48
7.2.180.0070.00714.55
7.2.170.0040.01114.59
7.2.160.0070.00714.76
7.2.150.0100.00316.70
7.2.140.0060.00616.55
7.2.130.0070.00416.32
7.2.120.0060.00616.47
7.2.110.0060.00916.58
7.2.100.0070.00716.29
7.2.90.0060.00916.41
7.2.80.0110.00716.54
7.2.70.0100.00716.71
7.2.60.0030.01316.68
7.2.50.0100.00616.54
7.2.40.0070.01016.37
7.2.30.0100.00716.61
7.2.20.0090.00316.32
7.2.10.0100.00616.41
7.2.00.0040.00716.59
7.1.330.0070.00715.64
7.1.320.0060.00315.55
7.1.310.0070.00415.63
7.1.300.0040.00415.53
7.1.290.0090.00615.55
7.1.280.0000.01615.27
7.1.270.0040.01115.55
7.1.260.0040.00815.39
7.1.250.0040.00415.42
7.1.240.0040.01115.55
7.1.230.0060.00615.50
7.1.220.0070.00415.66
7.1.210.0070.00715.66
7.1.200.0070.01015.29
7.1.190.0110.00515.05
7.1.180.0100.00715.43
7.1.170.0090.00615.39
7.1.160.0000.01315.39
7.1.150.0080.00415.44
7.1.140.0070.00715.66
7.1.130.0080.00815.43
7.1.120.0060.00615.48
7.1.110.0060.00615.02
7.1.100.0060.00615.45
7.1.90.0040.01215.68
7.1.80.0000.01715.36
7.1.70.0000.00915.41
7.1.60.0070.00715.42
7.1.50.0060.01015.36
7.1.40.0030.01015.47
7.1.30.0000.01515.44
7.1.20.0040.01115.14
7.1.10.0000.01115.45
7.1.00.0100.00315.66
7.0.330.0040.00415.21
7.0.320.0080.00815.02
7.0.310.0060.00915.00
7.0.300.0080.00814.89
7.0.290.0040.01215.35
7.0.280.0040.00814.93
7.0.270.0060.00614.71
7.0.260.0030.00614.97
7.0.250.0050.00515.05
7.0.240.0000.01414.83
7.0.230.0040.00915.32
7.0.220.0030.01015.10
7.0.210.0030.00715.21
7.0.200.0030.01315.29
7.0.190.0070.00715.16
7.0.180.0000.00815.16
7.0.170.0030.01014.94
7.0.160.0100.00615.25
7.0.150.0070.01015.24
7.0.140.0060.00815.14
7.0.130.0030.00615.46
7.0.120.0000.00915.26
7.0.110.0100.00715.33
7.0.100.0170.00015.16
7.0.90.0040.01515.28
7.0.80.0100.00315.18
7.0.70.0000.01715.20
7.0.60.0040.01515.11
7.0.50.0080.00815.10
7.0.40.0090.00613.11
7.0.30.0120.00613.34
7.0.20.0140.00513.20
7.0.10.0100.01013.46
7.0.00.0050.01012.93
5.6.400.0030.00614.05
5.6.390.0040.01213.74
5.6.380.0030.01614.34
5.6.370.0030.01414.46
5.6.360.0080.00414.23
5.6.350.0070.01314.23
5.6.340.0060.00914.27
5.6.330.0040.01114.35
5.6.320.0130.00314.06
5.6.310.0060.00614.43
5.6.300.0060.00614.22
5.6.290.0070.00714.13
5.6.280.0090.00614.51
5.6.270.0080.00614.26
5.6.260.0070.01014.25
5.6.250.0030.01414.18
5.6.240.0060.01214.06
5.6.230.0080.01114.19
5.6.220.0110.00714.14
5.6.210.0090.00914.12
5.6.200.0130.00613.88
5.6.190.0000.01713.70
5.6.180.0060.01213.89
5.6.170.0030.01013.88
5.6.160.0060.01213.98
5.6.150.0040.01514.13
5.6.140.0120.00814.09
5.6.130.0090.00514.18
5.6.120.0070.01113.66
5.6.110.0070.01113.77
5.6.100.0070.01314.10
5.6.90.0000.01813.85
5.6.80.0060.00914.16
5.6.70.0060.01513.99
5.6.60.0080.01214.10
5.6.50.0060.01614.16
5.6.40.0140.00714.30
5.6.30.0120.00914.20
5.6.20.0100.01013.91
5.6.10.0090.01014.13
5.6.00.0040.01814.14
5.5.380.0070.00314.16
5.5.370.0060.00914.04
5.5.360.0060.00913.77
5.5.350.0060.00913.96
5.5.340.0120.00314.27
5.5.330.0040.01114.00
5.5.320.0030.01214.11
5.5.310.0030.01214.04
5.5.300.0040.01514.34
5.5.290.0070.00714.06
5.5.280.0030.01013.95
5.5.270.0090.00914.03
5.5.260.0140.00313.49
5.5.250.0060.00913.85
5.5.240.0060.00913.91
5.5.230.0040.01214.30
5.5.220.0040.01113.82
5.5.210.0000.01413.80
5.5.200.0110.00813.71
5.5.190.0150.01013.83
5.5.180.0130.00813.96
5.5.170.0030.01413.93
5.5.160.0250.00013.89
5.5.150.0000.02013.97
5.5.140.0030.01314.19
5.5.130.0130.00313.62
5.5.120.0100.00713.88
5.5.110.0060.01014.11
5.5.100.0060.01014.08
5.5.90.0060.00314.01
5.5.80.0030.01314.22
5.5.70.0090.00613.82
5.5.60.0030.01013.95
5.5.50.0070.00714.12
5.5.40.0080.00614.11
5.5.30.0000.01513.68
5.5.20.0070.00714.16
5.5.10.0130.01616.89
5.5.00.0080.02616.96
5.4.450.0000.01310.86
5.4.440.0000.00810.84
5.4.430.0080.00311.08
5.4.420.0070.00310.86
5.4.410.0060.00610.57
5.4.400.0070.00310.81
5.4.390.0030.01210.59
5.4.380.0060.00610.68
5.4.370.0060.00810.89
5.4.360.0030.00711.07
5.4.350.0040.00710.79
5.4.340.0030.00910.90
5.4.330.0060.00010.88
5.4.320.0070.00710.87
5.4.310.0040.01110.79
5.4.300.0030.01010.88
5.4.290.0070.00410.53
5.4.280.0000.01310.55
5.4.270.0000.01210.93
5.4.260.0040.00410.95
5.4.250.0030.00610.80
5.4.240.0030.00610.42
5.4.230.0060.00310.72
5.4.220.0040.00410.71
5.4.210.0060.00610.99
5.4.200.0030.00310.69
5.4.190.0000.00910.61
5.4.180.0000.00810.39
5.4.170.0080.04214.81
5.4.160.0080.04314.94
5.4.150.0110.03814.79
5.4.140.0020.04513.80
5.4.130.0020.03513.72
5.4.120.0050.02013.63
5.4.110.0050.01913.53
5.4.100.0100.03013.72
5.4.90.0010.04413.62
5.4.80.0060.01813.54
5.4.70.0050.02213.49
5.4.60.0020.02213.74
5.4.50.0090.02513.55
5.4.40.0040.02513.61
5.4.30.0100.01513.64
5.4.20.0060.03513.64
5.4.10.0070.01813.60
5.4.00.0050.02513.11
5.3.290.0060.00610.51
5.3.280.0060.00610.38
5.3.270.0000.04312.53
5.3.260.0070.02412.48
5.3.250.0120.03912.48
5.3.240.0020.03012.53
5.3.230.0060.02312.51
5.3.220.0060.02412.45
5.3.210.0040.02112.59
5.3.200.0020.04812.41
5.3.190.0070.02112.58
5.3.180.0100.03912.52
5.3.170.0080.02612.61
5.3.160.0140.03512.49
5.3.150.0080.03812.58
5.3.140.0040.03412.54
5.3.130.0080.04012.42
5.3.120.0100.04012.42
5.3.110.0070.04112.57
5.3.100.0100.03812.20
5.3.90.0030.04412.28
5.3.80.0070.03612.25
5.3.70.0070.02512.26
5.3.60.0030.04012.29
5.3.50.0070.04012.17
5.3.40.0010.02312.16
5.3.30.0030.04212.23
5.3.20.0060.03612.12
5.3.10.0070.03612.03
5.3.00.0080.04011.90
5.2.170.0070.06311.20
5.2.160.0000.06311.36
5.2.150.0000.03311.38
5.2.140.0000.03311.32
5.2.130.0030.06311.23
5.2.120.0030.03011.29
5.2.110.0030.05011.34
5.2.100.0100.03011.28
5.2.90.0070.05311.37
5.2.80.0030.06011.27
5.2.70.0130.04711.12
5.2.60.0030.03011.18
5.2.50.0000.03011.20
5.2.40.0070.02311.21
5.2.30.0030.03010.95
5.2.20.0070.05711.10
5.2.10.0030.04010.84
5.2.00.0070.05010.86
5.1.60.0070.05010.24
5.1.50.0100.03310.21
5.1.40.0030.05310.19
5.1.30.0030.04310.50
5.1.20.0030.02710.61
5.1.10.0030.05010.28
5.1.00.0070.03710.05
5.0.50.0070.0378.63
5.0.40.0030.0438.46
5.0.30.0030.0538.46
5.0.20.0030.0208.46
5.0.10.0030.0208.46
5.0.00.0000.0338.46
4.4.90.0070.0278.46
4.4.80.0000.0308.46
4.4.70.0030.0378.46
4.4.60.0000.0408.46
4.4.50.0000.0308.46
4.4.40.0000.0238.46
4.4.30.0030.0378.46
4.4.20.0070.0308.46
4.4.10.0030.0338.46
4.4.00.0030.0538.46
4.3.110.0030.0308.46
4.3.100.0030.0338.46
4.3.90.0030.0338.46
4.3.80.0030.0478.46
4.3.70.0030.0278.46
4.3.60.0030.0408.46
4.3.50.0000.0208.46
4.3.40.0030.0238.46
4.3.30.0030.0208.46
4.3.20.0000.0278.46
4.3.10.0000.0138.46
4.3.00.0030.0178.46

preferences:
41.89 ms | 401 KiB | 5 Q