3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface IDBResult { function FetchAssoc(); function Rewind(); function NumRows(); } class DBResult implements IDBResult { private $_records = array( array('abc' => 'def'), array('foo' => 'bar'), array('fizz' => 'buzz'), ); private $_i = 0; function FetchAssoc() { return isset($this->_records[$this->_i]) ? $this->_records[$this->_i++] : null; } function Rewind() { $this->_i = 0; } function NumRows() { return count($this->_records); } } class ResultIterator implements Iterator { private $_rs; private $_rec; private $_index = 0; private $_onLoad; function __construct(IDBResult $rs, callable $callback = null) { $this->_rs = $rs; $this->_onLoad = $callback ?: function($rec, $i) { return $rec; }; } public function current() { return call_user_func_array($this->_onLoad, array($this->_rec, $this->_index)); } public function key() { return $this->_index; } public function next() { $this->_rec = $this->_rs->FetchAssoc(); $this->_index++; } public function rewind() { $this->_index = 0; $this->_rs->Rewind(); $this->_rec = $this->_rs->FetchAssoc(); } public function valid() { return $this->_index < $this->_rs->NumRows(); } } $rs = new DBResult(); $it = new ResultIterator($rs, function($rec, $i) { return array($i, key($rec), $rec[key($rec)]); }); foreach($it as $rec) { print_r($rec); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/OmZUa
function name:  (null)
number of ops:  19
compiled vars:  !0 = $rs, !1 = $it, !2 = $rec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   DECLARE_CLASS                                            'dbresult'
   29     1        DECLARE_CLASS                                            'resultiterator'
   66     2        NEW                                              $3      'DBResult'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $3
   67     5        NEW                                              $6      'ResultIterator'
          6        SEND_VAR_EX                                              !0
          7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FOmZUa%3A67%243'
          8        SEND_VAL_EX                                              ~7
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $6
   68    11      > FE_RESET_R                                       $10     !1, ->17
         12    > > FE_FETCH_R                                               $10, !2, ->17
   69    13    >   INIT_FCALL                                               'print_r'
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                                 
   68    16      > JMP                                                      ->12
         17    >   FE_FREE                                                  $10
   70    18      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FOmZUa%3A38%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $rec, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2      > RETURN                                                   !0
          3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FOmZUa%3A38%241

Function %00%7Bclosure%7D%2Fin%2FOmZUa%3A67%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $rec, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        INIT_ARRAY                                       ~2      !1
          3        INIT_FCALL                                               'key'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $3      
          6        ADD_ARRAY_ELEMENT                                ~2      $3
          7        INIT_FCALL                                               'key'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $4      
         10        FETCH_DIM_R                                      ~5      !0, $4
         11        ADD_ARRAY_ELEMENT                                ~2      ~5
         12      > RETURN                                                   ~2
         13*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FOmZUa%3A67%243

Class IDBResult:
Function fetchassoc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  FetchAssoc
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E > > RETURN                                                   null

End of function fetchassoc

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  Rewind
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E > > RETURN                                                   null

End of function rewind

Function numrows:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  NumRows
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E > > RETURN                                                   null

End of function numrows

End of class IDBResult.

Class DBResult:
Function fetchassoc:
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 = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  FetchAssoc
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_OBJ_R                                      ~1      '_i'
          1        FETCH_OBJ_IS                                     ~0      '_records'
          2        ISSET_ISEMPTY_DIM_OBJ                         0          ~0, ~1
          3      > JMPZ                                                     ~2, ->9
          4    >   POST_INC_OBJ                                     ~4      '_i'
          5        FETCH_OBJ_R                                      ~3      '_records'
          6        FETCH_DIM_R                                      ~5      ~3, ~4
          7        QM_ASSIGN                                        ~6      ~5
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~6      null
         10    > > RETURN                                                   ~6
   20    11*     > RETURN                                                   null

End of function fetchassoc

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  Rewind
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN_OBJ                                               '_i'
          1        OP_DATA                                                  0
   23     2      > RETURN                                                   null

End of function rewind

Function numrows:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  NumRows
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_OBJ_R                                      ~0      '_records'
          1        COUNT                                            ~1      ~0
          2      > RETURN                                                   ~1
   26     3*     > RETURN                                                   null

End of function numrows

End of class DBResult.

Class ResultIterator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  __construct
number of ops:  10
compiled vars:  !0 = $rs, !1 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   37     2        ASSIGN_OBJ                                               '_rs'
          3        OP_DATA                                                  !0
   38     4        JMP_SET                                          ~4      !1, ->7
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FOmZUa%3A38%241'
          6        QM_ASSIGN                                        ~4      ~5
          7        ASSIGN_OBJ                                               '_onLoad'
          8        OP_DATA                                                  ~4
   39     9      > RETURN                                                   null

End of function __construct

Function current:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  current
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   FETCH_OBJ_R                                      ~0      '_onLoad'
          1        INIT_USER_CALL                                0          'call_user_func_array', ~0
          2        FETCH_OBJ_R                                      ~1      '_rec'
          3        INIT_ARRAY                                       ~2      ~1
          4        FETCH_OBJ_R                                      ~3      '_index'
          5        ADD_ARRAY_ELEMENT                                ~2      ~3
          6        SEND_ARRAY                                               ~2
          7        CHECK_UNDEF_ARGS                                         
          8        DO_FCALL                                      0  $4      
          9      > RETURN                                                   $4
   43    10*     > RETURN                                                   null

End of function current

Function key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  key
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   FETCH_OBJ_R                                      ~0      '_index'
          1      > RETURN                                                   ~0
   47     2*     > RETURN                                                   null

End of function key

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  next
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   FETCH_OBJ_R                                      ~1      '_rs'
          1        INIT_METHOD_CALL                                         ~1, 'FetchAssoc'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN_OBJ                                               '_rec'
          4        OP_DATA                                                  $2
   51     5        PRE_INC_OBJ                                              '_index'
   52     6      > RETURN                                                   null

End of function next

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  rewind
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   ASSIGN_OBJ                                               '_index'
          1        OP_DATA                                                  0
   56     2        FETCH_OBJ_R                                      ~1      '_rs'
          3        INIT_METHOD_CALL                                         ~1, 'Rewind'
          4        DO_FCALL                                      0          
   57     5        FETCH_OBJ_R                                      ~4      '_rs'
          6        INIT_METHOD_CALL                                         ~4, 'FetchAssoc'
          7        DO_FCALL                                      0  $5      
          8        ASSIGN_OBJ                                               '_rec'
          9        OP_DATA                                                  $5
   58    10      > RETURN                                                   null

End of function rewind

Function valid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OmZUa
function name:  valid
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   FETCH_OBJ_R                                      ~0      '_index'
          1        FETCH_OBJ_R                                      ~1      '_rs'
          2        INIT_METHOD_CALL                                         ~1, 'NumRows'
          3        DO_FCALL                                      0  $2      
          4        IS_SMALLER                                       ~3      ~0, $2
          5      > RETURN                                                   ~3
   62     6*     > RETURN                                                   null

End of function valid

End of class ResultIterator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.91 ms | 1400 KiB | 17 Q