3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 offsetSet($key, $value) { $this->throwEx(array('Do not directly set indices in a %s, use push() instead', __CLASS__)); return false; } 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 7.0.0 - 7.1.0
object(ringBuffer)#1 (3) { ["data":"ringBuffer":private]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current":"ringBuffer":private]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught Exception in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx(Array) #1 /in/X3OYM(91): ringBuffer->offsetGet('foo') #2 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
object(ringBuffer)#1 (3) { ["data":"ringBuffer":private]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current":"ringBuffer":private]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' with message '' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx() #1 /in/X3OYM(91): ringBuffer->offsetGet() #2 {main}
Process exited with code 255.
Output for 5.3.0 - 5.6.28
object(ringBuffer)#1 (3) { ["data":"ringBuffer":private]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current":"ringBuffer":private]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx(Array) #1 /in/X3OYM(91): ringBuffer->offsetGet('foo') #2 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 5.1.3 - 5.2.17
object(ringBuffer)#1 (3) { ["data:private"]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current:private"]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx(Array) #1 /in/X3OYM(91): ringBuffer->offsetGet('foo') #2 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 5.0.5 - 5.1.2
object(ringBuffer)#1 (3) { ["data:private"]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current:private"]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx(Array) #1 /in/X3OYM(91): ringBuffer::offsetGet('foo') #2 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 5.0.4
object(ringBuffer)#1 (3) { ["data:private"]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current:private"]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(51): ringBuffer::throwEx() #1 /in/X3OYM(25): ringBuffer->throwEx(Array) #2 /in/X3OYM(91): ringBuffer::offsetGet() #3 /in/X3OYM(91): unknown() #4 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 5.0.3
object(ringBuffer)#1 (3) { ["data:private"]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current:private"]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(51): ringBuffer::throwEx() #1 /in/X3OYM(25): ringBuffer->throwEx(Array) #2 /in/X3OYM(91): ringBuffer::offsetGet() #3 /in/X3OYM(91): unknown() #4 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 5.0.0 - 5.0.2
object(ringBuffer)#1 (3) { ["data:private"]=> array(2) { [0]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "baz" } [1]=> array(3) { ["s"]=> bool(true) ["d"]=> bool(true) ["v"]=> string(3) "bar" } } ["current:private"]=> int(1) ["size"]=> int(2) } Notice: Undefined variable: args0 in /in/X3OYM on line 48 Notice: Undefined variable: msg in /in/X3OYM on line 51 Fatal error: Uncaught exception 'Exception' in /in/X3OYM:51 Stack trace: #0 /in/X3OYM(25): ringBuffer->throwEx(Array) #1 /in/X3OYM(91): ringBuffer::offsetGet() #2 /in/X3OYM(91): unknown() #3 {main} thrown in /in/X3OYM on line 51
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/X3OYM on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/X3OYM on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/X3OYM on line 3
Process exited with code 255.