3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ringBuffer implements ArrayAccess { private $data; private $current = 0; public $size; public function __construct($sz=10) { $this->size = $sz; foreach(range(1, $sz) as $k=>$v) { /* s: whether this node is set; d: whether this node is "dirty" (has been read since it was last set); v: the value */ $this->data[$k] = array('s' => false, 'd' => false, 'v' => NULL); } } public function offsetGet($key) { if (! is_int($key)) $this->throwEx(array("offset '%s' must be an integer, not %s", $key, gettype($key))); if ($key > $this->size) $key %= $this->size; if (!isset($this->data[$key])) return false; if (!$this->data[$key]['s']) return false; $this->data[$key]['d'] = false; return $this->data[$key]['v']; } public function offsetExists($key) { if (! is_int($key)) throw new Exception(); if ($key > $this->size) return $this->data[$key]['s']; } public function offsetUnset($key) { $this->data[$key]['s'] = false; $this->data[$key]['v'] = NULL; } private function throwEx() { $args = func_get_args(); if (is_array($args0)) { $msg = call_user_func_array('sprintf', $args0); } throw new Exception($msg); } public function push($value) { if ($this->current >= $this->size) $this->current %= $this->size; $this->data[$this->current] = array('s'=>true, 'd'=>true, 'v' => $value); $this->current++; return $value; } } $a = new ringBuffer(2); $a->push("foo"); $a->push("bar"); $a->push("baz"); var_dump($a); /* Will throw an exception because you can't directly set indices in a ringBuffer */ $a0 = 'foo'; /* Will throw an exception because ringBuffer indices must be ints */ var_dump($a['foo']); var_dump($a[1.0]);
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of ringBuffer::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DKbvM on line 29 Deprecated: Return type of ringBuffer::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DKbvM on line 19 Deprecated: Return type of ringBuffer::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DKbvM on line 36 Fatal error: Class ringBuffer contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ArrayAccess::offsetSet) in /in/DKbvM on line 2
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
50.51 ms | 402 KiB | 8 Q