3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class a implements \Iterator, \Countable, \ArrayAccess { private $a; private $b; private $c; private $keys = [ 'a', 'b', 'c' ]; private $pointer; public function __construct($a = null, $b = null, $c = null) { $this->a = $a; $this->b = $b; $this->c = $c; } public function count() { $pointer = 0; foreach ($this as $element) { if ($element) { $pointer++; } } return $pointer; } public function current() { $key = $this->key(); return $this->{$key}; } public function key() { return $this->keys[$this->pointer]; } public function next() { $this->pointer++; } public function valid() { return $this->pointer < count($this->keys); } public function rewind() { $this->pointer = 0; } public function offsetExists($offset) { return isset($this->{$offset}); } public function offsetGet($offset) { return $this->{$offset}; } public function offsetSet($offset, $value) { throw new \BadMethodCallException('Object does not implement set'); } public function offsetUnset($offset) { throw new \BadMethodCallException('Value object does not implement modification via unset'); } } $me = new a(1,2,3); var_dump($me->count()); foreach ($me as $key => $el) { if ($key === 'b') { $me[$key] = 111; } } var_dump($me);
based on vCf3q
Output for 7.0.0 - 7.2.0
int(3) Fatal error: Uncaught BadMethodCallException: Object does not implement set in /in/eMc5N:76 Stack trace: #0 /in/eMc5N(94): a->offsetSet('b', 111) #1 {main} thrown in /in/eMc5N on line 76
Process exited with code 255.
Output for 5.4.2 - 5.6.28
int(3) Fatal error: Uncaught exception 'BadMethodCallException' with message 'Object does not implement set' in /in/eMc5N:76 Stack trace: #0 /in/eMc5N(94): a->offsetSet('b', 111) #1 {main} thrown in /in/eMc5N on line 76
Process exited with code 255.
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/eMc5N on line 11
Process exited with code 255.