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 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]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X3OYM
function name:  (null)
number of ops:  27
compiled vars:  !0 = $a, !1 = $a0
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'ringbuffer'
   63     1        NEW                                              $2      'ringBuffer'
          2        SEND_VAL_EX                                              2
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
   68     5        INIT_METHOD_CALL                                         !0, 'push'
          6        SEND_VAL_EX                                              'foo'
          7        DO_FCALL                                      0          
   70     8        INIT_METHOD_CALL                                         !0, 'push'
          9        SEND_VAL_EX                                              'bar'
         10        DO_FCALL                                      0          
   72    11        INIT_METHOD_CALL                                         !0, 'push'
         12        SEND_VAL_EX                                              'baz'
         13        DO_FCALL                                      0          
   77    14        INIT_FCALL                                               'var_dump'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                                 
   84    17        ASSIGN                                                   !1, 'foo'
   91    18        INIT_FCALL                                               'var_dump'
         19        FETCH_DIM_R                                      ~10     !0, 'foo'
         20        SEND_VAL                                                 ~10
         21        DO_ICALL                                                 
   93    22        INIT_FCALL                                               'var_dump'
         23        FETCH_DIM_R                                      ~12     !0, 1
         24        SEND_VAL                                                 ~12
         25        DO_ICALL                                                 
         26      > RETURN                                                   1

Class ringBuffer:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/X3OYM
function name:  __construct
number of ops:  16
compiled vars:  !0 = $sz, !1 = $v, !2 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV_INIT                                        !0      10
   12     1        ASSIGN_OBJ                                               'size'
          2        OP_DATA                                                  !0
   13     3        INIT_FCALL                                               'range'
          4        SEND_VAL                                                 1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $4      
          7      > FE_RESET_R                                       $5      $4, ->14
          8    > > FE_FETCH_R                                       ~6      $5, !1, ->14
          9    >   ASSIGN                                                   !2, ~6
   15    10        FETCH_OBJ_W                                      $8      'data'
         11        ASSIGN_DIM                                               $8, !2
         12        OP_DATA                                                  <array>
   13    13      > JMP                                                      ->8
         14    >   FE_FREE                                                  $5
   17    15      > RETURN                                                   null

End of function __construct

Function offsetset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X3OYM
function name:  offsetSet
number of ops:  7
compiled vars:  !0 = $key, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   20     2        INIT_METHOD_CALL                                         'throwEx'
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0          
   21     5      > RETURN                                                   <false>
   22     6*     > RETURN                                                   null

End of function offsetset

Function offsetget:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 27
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 11
filename:       /in/X3OYM
function name:  offsetGet
number of ops:  36
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        TYPE_CHECK                                   16  ~1      !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->11
          4    >   INIT_METHOD_CALL                                         'throwEx'
          5        INIT_ARRAY                                       ~3      'offset+%27%25s%27+must+be+an+integer%2C+not+%25s'
          6        ADD_ARRAY_ELEMENT                                ~3      !0
          7        GET_TYPE                                         ~4      !0
          8        ADD_ARRAY_ELEMENT                                ~3      ~4
          9        SEND_VAL_EX                                              ~3
         10        DO_FCALL                                      0          
   26    11    >   FETCH_OBJ_R                                      ~6      'size'
         12        IS_SMALLER                                               ~6, !0
         13      > JMPZ                                                     ~7, ->16
         14    >   FETCH_OBJ_R                                      ~8      'size'
         15        ASSIGN_OP                                     5          !0, ~8
   27    16    >   FETCH_OBJ_IS                                     ~10     'data'
         17        ISSET_ISEMPTY_DIM_OBJ                         0  ~11     ~10, !0
         18        BOOL_NOT                                         ~12     ~11
         19      > JMPZ                                                     ~12, ->21
         20    > > RETURN                                                   <false>
   28    21    >   FETCH_OBJ_R                                      ~13     'data'
         22        FETCH_DIM_R                                      ~14     ~13, !0
         23        FETCH_DIM_R                                      ~15     ~14, 's'
         24        BOOL_NOT                                         ~16     ~15
         25      > JMPZ                                                     ~16, ->27
         26    > > RETURN                                                   <false>
   30    27    >   FETCH_OBJ_W                                      $17     'data'
         28        FETCH_DIM_W                                      $18     $17, !0
         29        ASSIGN_DIM                                               $18, 'd'
         30        OP_DATA                                                  <false>
   31    31        FETCH_OBJ_R                                      ~20     'data'
         32        FETCH_DIM_R                                      ~21     ~20, !0
         33        FETCH_DIM_R                                      ~22     ~21, 'v'
         34      > RETURN                                                   ~22
   32    35*     > RETURN                                                   null

End of function offsetget

Function offsetexists:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X3OYM
function name:  offsetExists
number of ops:  15
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        TYPE_CHECK                                   16  ~1      !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->7
   36     4    >   NEW                                              $3      'Exception'
          5        DO_FCALL                                      0          
          6      > THROW                                         0          $3
   37     7    >   FETCH_OBJ_R                                      ~5      'size'
          8        IS_SMALLER                                               ~5, !0
          9      > JMPZ                                                     ~6, ->14
   38    10    >   FETCH_OBJ_R                                      ~7      'data'
         11        FETCH_DIM_R                                      ~8      ~7, !0
         12        FETCH_DIM_R                                      ~9      ~8, 's'
         13      > RETURN                                                   ~9
   39    14    > > RETURN                                                   null

End of function offsetexists

Function offsetunset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X3OYM
function name:  offsetUnset
number of ops:  10
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        FETCH_OBJ_W                                      $1      'data'
          2        FETCH_DIM_W                                      $2      $1, !0
          3        ASSIGN_DIM                                               $2, 's'
          4        OP_DATA                                                  <false>
   43     5        FETCH_OBJ_W                                      $4      'data'
          6        FETCH_DIM_W                                      $5      $4, !0
          7        ASSIGN_DIM                                               $5, 'v'
          8        OP_DATA                                                  null
   44     9      > RETURN                                                   null

End of function offsetunset

Function throwex:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
filename:       /in/X3OYM
function name:  throwEx
number of ops:  14
compiled vars:  !0 = $args, !1 = $args0, !2 = $msg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   FUNC_GET_ARGS                                    ~3      
          1        ASSIGN                                                   !0, ~3
   48     2        TYPE_CHECK                                  128          !1
          3      > JMPZ                                                     ~5, ->9
   49     4    >   INIT_FCALL                                               'sprintf'
          5        SEND_ARRAY                                               !1
          6        CHECK_UNDEF_ARGS                                         
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !2, $6
   51     9    >   NEW                                              $8      'Exception'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $8
   52    13*     > RETURN                                                   null

End of function throwex

Function push:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/X3OYM
function name:  push
number of ops:  18
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        FETCH_OBJ_R                                      ~1      'current'
          2        FETCH_OBJ_R                                      ~2      'size'
          3        IS_SMALLER_OR_EQUAL                                      ~2, ~1
          4      > JMPZ                                                     ~3, ->8
          5    >   FETCH_OBJ_R                                      ~5      'size'
          6        ASSIGN_OBJ_OP                                 5          'current'
          7        OP_DATA                                                  ~5
   56     8    >   FETCH_OBJ_R                                      ~7      'current'
          9        INIT_ARRAY                                       ~9      <true>, 's'
         10        ADD_ARRAY_ELEMENT                                ~9      <true>, 'd'
         11        ADD_ARRAY_ELEMENT                                ~9      !0, 'v'
         12        FETCH_OBJ_W                                      $6      'data'
         13        ASSIGN_DIM                                               $6, ~7
         14        OP_DATA                                                  ~9
   57    15        PRE_INC_OBJ                                              'current'
   58    16      > RETURN                                                   !0
   59    17*     > RETURN                                                   null

End of function push

End of class ringBuffer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.16 ms | 1412 KiB | 19 Q