3v4l.org

run code in 300+ PHP versions simultaneously
<?php class States implements \ArrayAccess { private $states = []; /** * @param mixed $offset * * @return bool */ public function offsetExists($offset) { return array_key_exists($offset, $this->states); } /** * @param mixed $offset * * @throws UnknownStateException * * @return AllianceState */ public function offsetGet($offset) { if ( ! $this->offsetExists($offset)) { throw new UnknownStateException(); } return $this->states[$offset]; } /** * @param mixed $offset * @param mixed $value * * @throws InvalidArgumentException */ public function offsetSet($offset, $value) { $offset = count($this->states); $this->states[$offset] = $value; } /** * @param mixed $offset * * @throws UnknownStateException */ public function offsetUnset($offset) { unset($this->states[$offset]); $this->states = array_values($this->states); } } $states = new States(); $states->add('foo'); $states->add('bar'); var_dump($states);

preferences:
43.84 ms | 402 KiB | 5 Q