3v4l.org

run code in 300+ PHP versions simultaneously
<?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']);
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Return type of B::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/89YJo on line 22 Deprecated: Return type of B::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/89YJo on line 34 Deprecated: Return type of B::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/89YJo on line 16 Deprecated: Return type of B::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/89YJo on line 28 Fatal error: Uncaught Exception: id property does not exists in /in/89YJo:40 Stack trace: #0 /in/89YJo(57): B->offsetGet('id') #1 {main} thrown in /in/89YJo on line 40
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Fatal error: Uncaught Exception: id property does not exists in /in/89YJo:40 Stack trace: #0 /in/89YJo(57): B->offsetGet('id') #1 {main} thrown in /in/89YJo on line 40
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Uncaught exception 'Exception' with message 'id property does not exists' in /in/89YJo:40 Stack trace: #0 /in/89YJo(57): B->offsetGet('id') #1 {main} thrown in /in/89YJo on line 40
Process exited with code 255.

preferences:
183.82 ms | 402 KiB | 194 Q