3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyEnum { private $values = [ 'A' => 1, 'B' => 2, 'C' => 3, ]; private $key = null; public function __construct($key) { if (!isset($this->values[$key])) { throw new Exception('Incorrect Enum Value'); } $this->key = $key; } public function key(): string { return $this->key; } public function val(): int { return $this->values[$this->key]; } } function foo(MyEnum $foo) { echo $foo->val().PHP_EOL; } // This works foo(new MyEnum('A')); try { // Rejected, don't allow passing the underlying value foo(1); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; } try { // Rejected, value not part of the enum foo(new MyEnum('D')); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; }
Output for git.master, git.master_jit, rfc.property-hooks
1 foo(): Argument #1 ($foo) must be of type MyEnum, int given, called in /in/bBpRu on line 39 Incorrect Enum Value

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
56.31 ms | 401 KiB | 8 Q