3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Dictionary { private $kMap; private $vMap; private $kvHashMap; private $vkHashMap; public function add($k, $v) { $kHash = spl_object_hash($k); $vHash = spl_object_hash($v); $this->kMap[$kHash] = $k; $this->vMap[$vHash] = $v; $this->kvHashMap[$kHash] = $vHash; $this->vkHashMap[$vHash] = $kHash; } public function get($k) { $kHash = spl_object_hash($k); if (isset($this->kvHashMap[$kHash])) { $vHash = $this->kvHashMap[$kHash]; if (isset($this->vMap[$vHash])) { return $this->vMap[$vHash]; } } return null; } public function getKey($v) { $vHash = spl_object_hash($v); if (isset($this->vkHashMap[$vHash])) { $kHash = $this->vkHashMap[$vHash]; if (isset($this->kMap[$kHash])) { return $this->kMap[$kHash]; } } return null; } } $dictionary = new Dictionary(); $k = (object) ['i' => 'am a key']; $v = (object) ['i' => 'am a value']; $dictionary->add($k, $v); var_dump($dictionary->get($k)); var_dump($dictionary->getKey($v));
Output for 5.4.0 - 7.1.0
object(stdClass)#3 (1) { ["i"]=> string(10) "am a value" } object(stdClass)#2 (1) { ["i"]=> string(8) "am a key" }
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/W8qch on line 51
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/W8qch on line 51
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/W8qch on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/W8qch on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/W8qch on line 6
Process exited with code 255.