3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Mapper { private $from; public function __construct($from) { $this->from = $from; } public function __invoke($key) { if (is_object($key)) foreach ($this->from as $k => $v) if ($v === $key) return $k; return $this->from[$key]; } } /* enum Suit { H, C, D, S } */ abstract class Suit { } $H = new class extends Suit { }; $C = new class extends Suit { }; $D = new class extends Suit { }; $S = new class extends Suit { }; $dbMapper = new Mapper([ 1 => $H, 2 => $C, 3 => $D, 4 => $S ]); $jsonMapper = new Mapper([ 'h' => $H, 'c' => $C, 'd' => $D, 's' => $S ]); var_dump($dbMapper(1)); var_dump($dbMapper($H)); var_dump($jsonMapper('c')); var_dump($jsonMapper($C));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
object(Suit@anonymous)#1 (0) { } int(1) object(Suit@anonymous)#2 (0) { } string(1) "c"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 object(Suit@anonymous)#1 (0) { } int(1) object(Suit@anonymous)#2 (0) { } string(1) "c"
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
object(class@anonymous)#1 (0) { } int(1) object(class@anonymous)#2 (0) { } string(1) "c"

preferences:
121.06 ms | 402 KiB | 183 Q