3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace App\Localization; enum Locale { case ru; case en; public static function tryFromName(string $name): ?self { $constant = self::class.'::'.$name; if (\defined($constant)) { return \constant($constant); } return null; } public static function fromName(string $name): self { return self::tryFromName($name) ?? throw new \ValueError(sprintf( '"%s" is not a valid name for enum "%s"', $name, self::class, )); } } var_dump(Locale::tryFromName('ru')); var_dump(Locale::tryFromName('fr')); var_dump(Locale::fromName('en')); var_dump(Locale::fromName('es'));
Output for 8.1.0 - 8.1.5
enum(App\Localization\Locale::ru) NULL enum(App\Localization\Locale::en) Fatal error: Uncaught ValueError: "es" is not a valid name for enum "App\Localization\Locale" in /in/EgBB1:24 Stack trace: #0 /in/EgBB1(35): App\Localization\Locale::fromName('es') #1 {main} thrown in /in/EgBB1 on line 24
Process exited with code 255.
Output for 8.0.1 - 8.0.18
Parse error: syntax error, unexpected identifier "Locale" in /in/EgBB1 on line 5
Process exited with code 255.
Output for 7.4.0 - 7.4.29
Parse error: syntax error, unexpected 'Locale' (T_STRING) in /in/EgBB1 on line 5
Process exited with code 255.

preferences:
31.72 ms | 411 KiB | 5 Q