3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TransformingIterator implements \Iterator { /** * @var Iterator */ private $inner; /** * @var callable */ private $valTransformCallback; /** * @var callable */ private $keyTransformCallback; /** * Constructor * * @param Iterator|array $inner * @param callable $valTransformCallback * @param callable $keyTransformCallback * @throws \InvalidArgumentException */ public function __construct($inner, callable $valTransformCallback = null, callable $keyTransformCallback = null) { if ($inner instanceof \Iterator) { $this->inner = $inner; } else if (is_array($inner)) { $this->inner = new \ArrayIterator($inner); } else { throw new \InvalidArgumentException('$inner must be an Iterator or an array'); } $this->valTransformCallback = $valTransformCallback; $this->keyTransformCallback = $keyTransformCallback; } public function current() { return $this->valTransformCallback ? call_user_func($this->valTransformCallback, $this->inner->current()) : $this->inner->current(); } public function next() { $this->inner->next(); } public function key() { return $this->keyTransformCallback ? call_user_func($this->keyTransformCallback, $this->inner->key()) : $this->inner->key(); } public function valid() { return $this->inner->valid(); } public function rewind() { $this->inner->rewind(); } } $it = new TransformingIterator([1,2,3], function ($value) { return $value + 1; }, function ($value) { return $value - 1; }); foreach ($it as $key => $val) { echo $key . '=>' . $val . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 17
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/sC6JV
function name:  (null)
number of ops:  19
compiled vars:  !0 = $it, !1 = $val, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'transformingiterator'
   72     1        NEW                                              $3      'TransformingIterator'
          2        SEND_VAL_EX                                              <array>
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FsC6JV%3A72%241'
          4        SEND_VAL_EX                                              ~4
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FsC6JV%3A72%242'
          6        SEND_VAL_EX                                              ~5
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !0, $3
   74     9      > FE_RESET_R                                       $8      !0, ->17
         10    > > FE_FETCH_R                                       ~9      $8, !1, ->17
         11    >   ASSIGN                                                   !2, ~9
   75    12        CONCAT                                           ~11     !2, '%3D%3E'
         13        CONCAT                                           ~12     ~11, !1
         14        CONCAT                                           ~13     ~12, '%0A'
         15        ECHO                                                     ~13
   74    16      > JMP                                                      ->10
         17    >   FE_FREE                                                  $8
   76    18      > RETURN                                                   1

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

End of function %00%7Bclosure%7D%2Fin%2FsC6JV%3A72%241

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

End of function %00%7Bclosure%7D%2Fin%2FsC6JV%3A72%242

Class TransformingIterator:
Function __construct:
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 = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/sC6JV
function name:  __construct
number of ops:  25
compiled vars:  !0 = $inner, !1 = $valTransformCallback, !2 = $keyTransformCallback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      null
   30     3        INSTANCEOF                                               !0, 'Iterator'
          4      > JMPZ                                                     ~3, ->8
   31     5    >   ASSIGN_OBJ                                               'inner'
          6        OP_DATA                                                  !0
          7      > JMP                                                      ->20
   32     8    >   TYPE_CHECK                                  128          !0
          9      > JMPZ                                                     ~5, ->16
   33    10    >   NEW                                              $7      'ArrayIterator'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
         13        ASSIGN_OBJ                                               'inner'
         14        OP_DATA                                                  $7
         15      > JMP                                                      ->20
   35    16    >   NEW                                              $9      'InvalidArgumentException'
         17        SEND_VAL_EX                                              '%24inner+must+be+an+Iterator+or+an+array'
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $9
   38    20    >   ASSIGN_OBJ                                               'valTransformCallback'
         21        OP_DATA                                                  !1
   39    22        ASSIGN_OBJ                                               'keyTransformCallback'
         23        OP_DATA                                                  !2
   40    24      > RETURN                                                   null

End of function __construct

Function current:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sC6JV
function name:  current
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   FETCH_OBJ_R                                      ~0      'valTransformCallback'
          1      > JMPZ                                                     ~0, ->11
   45     2    >   FETCH_OBJ_R                                      ~1      'valTransformCallback'
          3        INIT_USER_CALL                                1          'call_user_func', ~1
          4        FETCH_OBJ_R                                      ~2      'inner'
          5        INIT_METHOD_CALL                                         ~2, 'current'
          6        DO_FCALL                                      0  $3      
          7        SEND_USER                                                $3
          8        DO_FCALL                                      0  $4      
          9        QM_ASSIGN                                        ~5      $4
         10      > JMP                                                      ->15
   46    11    >   FETCH_OBJ_R                                      ~6      'inner'
         12        INIT_METHOD_CALL                                         ~6, 'current'
         13        DO_FCALL                                      0  $7      
         14        QM_ASSIGN                                        ~5      $7
         15    > > RETURN                                                   ~5
   47    16*     > RETURN                                                   null

End of function current

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sC6JV
function name:  next
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'next'
          2        DO_FCALL                                      0          
   52     3      > RETURN                                                   null

End of function next

Function key:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sC6JV
function name:  key
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   FETCH_OBJ_R                                      ~0      'keyTransformCallback'
          1      > JMPZ                                                     ~0, ->11
   57     2    >   FETCH_OBJ_R                                      ~1      'keyTransformCallback'
          3        INIT_USER_CALL                                1          'call_user_func', ~1
          4        FETCH_OBJ_R                                      ~2      'inner'
          5        INIT_METHOD_CALL                                         ~2, 'key'
          6        DO_FCALL                                      0  $3      
          7        SEND_USER                                                $3
          8        DO_FCALL                                      0  $4      
          9        QM_ASSIGN                                        ~5      $4
         10      > JMP                                                      ->15
   58    11    >   FETCH_OBJ_R                                      ~6      'inner'
         12        INIT_METHOD_CALL                                         ~6, 'key'
         13        DO_FCALL                                      0  $7      
         14        QM_ASSIGN                                        ~5      $7
         15    > > RETURN                                                   ~5
   59    16*     > RETURN                                                   null

End of function key

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

End of function valid

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sC6JV
function name:  rewind
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'rewind'
          2        DO_FCALL                                      0          
   69     3      > RETURN                                                   null

End of function rewind

End of class TransformingIterator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.61 ms | 1407 KiB | 13 Q