3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class A { protected $id; protected $displayName; } class B implements ArrayAccess { protected $id; protected $displayName; public function offsetSet($offset, $value) { $offset = $this->_mapField($offset); $this->$offset = $value; } public function offsetExists($offset) { $offset = $this->_mapField($offset); return isset($this->$offset); } public function offsetUnset($offset) { $offset = $this->_mapField($offset); throw new \Exception("Cannot remove the property $offset"); } public function offsetGet($offset) { $offset = $this->_mapField($offset); if ($this->offsetExists($offset)) { //return $this->$offset; } throw new \Exception("$offset property does not exists"); } private function _mapField($keyFrom) { if ($keyFrom === 'id_user') { return 'id'; } elseif ($keyFrom === 'name') { return 'displayName'; } return $keyFrom; } } $b = new B(); var_dump($b['id']); var_dump($b['id_user']); var_dump($b['displayName']); var_dump($b['name']);
based on 89YJo
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Exception: id property does not exists in /in/6Eq0T:40 Stack trace: #0 /in/6Eq0T(57): B->offsetGet('id') #1 {main} thrown in /in/6Eq0T on line 40
Process exited with code 255.
Output for 5.3.22 - 5.6.28
Fatal error: Uncaught exception 'Exception' with message 'id property does not exists' in /in/6Eq0T:40 Stack trace: #0 /in/6Eq0T(57): B->offsetGet('id') #1 {main} thrown in /in/6Eq0T on line 40
Process exited with code 255.