3v4l.org

run code in 300+ PHP versions simultaneously
<?php class RewindableGenerator implements Iterator { private $rewound; private $iteratorConstructor; private $iterator; function __construct(callable $iteratorConstructor){ $this->iteratorConstructor = $iteratorConstructor; $this->rewind(); } function rewind(){ if(!$this->rewound){ $this->iterator = ($this->iteratorConstructor)(); $this->rewound = true; } } function next(){ $this->iterator->next(); $this->rewound = false; } function valid(){ return $this->iterator->valid(); } function key(){ return $this->iterator->key(); } function current(){ return $this->iterator->current(); } } function test(){ yield 0; yield 1; yield 2; yield 3; yield 4; } function baz(){ return new RewindableGenerator('test'); } $gen = baz(); echo $gen->current() . " 0\n"; $gen->next(); echo $gen->current() . " 1\n"; $gen->next(); echo $gen->current() . " 2\n"; $gen->next(); echo $gen->current() . " 3\n"; echo "rewind\n"; $gen->rewind(); echo $gen->current() . " 0\n"; $gen->next(); echo $gen->current() . " 1\n"; $gen->next(); echo $gen->current() . " 2\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YS6Ze
function name:  (null)
number of ops:  46
compiled vars:  !0 = $gen
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'rewindablegenerator'
   52     1        INIT_FCALL                                               'baz'
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   54     4        INIT_METHOD_CALL                                         !0, 'current'
          5        DO_FCALL                                      0  $3      
          6        CONCAT                                           ~4      $3, '+0%0A'
          7        ECHO                                                     ~4
   55     8        INIT_METHOD_CALL                                         !0, 'next'
          9        DO_FCALL                                      0          
   56    10        INIT_METHOD_CALL                                         !0, 'current'
         11        DO_FCALL                                      0  $6      
         12        CONCAT                                           ~7      $6, '+1%0A'
         13        ECHO                                                     ~7
   57    14        INIT_METHOD_CALL                                         !0, 'next'
         15        DO_FCALL                                      0          
   58    16        INIT_METHOD_CALL                                         !0, 'current'
         17        DO_FCALL                                      0  $9      
         18        CONCAT                                           ~10     $9, '+2%0A'
         19        ECHO                                                     ~10
   59    20        INIT_METHOD_CALL                                         !0, 'next'
         21        DO_FCALL                                      0          
   60    22        INIT_METHOD_CALL                                         !0, 'current'
         23        DO_FCALL                                      0  $12     
         24        CONCAT                                           ~13     $12, '+3%0A'
         25        ECHO                                                     ~13
   62    26        ECHO                                                     'rewind%0A'
   63    27        INIT_METHOD_CALL                                         !0, 'rewind'
         28        DO_FCALL                                      0          
   64    29        INIT_METHOD_CALL                                         !0, 'current'
         30        DO_FCALL                                      0  $15     
         31        CONCAT                                           ~16     $15, '+0%0A'
         32        ECHO                                                     ~16
   65    33        INIT_METHOD_CALL                                         !0, 'next'
         34        DO_FCALL                                      0          
   66    35        INIT_METHOD_CALL                                         !0, 'current'
         36        DO_FCALL                                      0  $18     
         37        CONCAT                                           ~19     $18, '+1%0A'
         38        ECHO                                                     ~19
   67    39        INIT_METHOD_CALL                                         !0, 'next'
         40        DO_FCALL                                      0          
   68    41        INIT_METHOD_CALL                                         !0, 'current'
         42        DO_FCALL                                      0  $21     
         43        CONCAT                                           ~22     $21, '+2%0A'
         44        ECHO                                                     ~22
         45      > RETURN                                                   1

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/YS6Ze
function name:  test
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   GENERATOR_CREATE                                         
   41     1        YIELD                                                    0
   42     2        YIELD                                                    1
   43     3        YIELD                                                    2
   44     4        YIELD                                                    3
   45     5        YIELD                                                    4
   46     6      > GENERATOR_RETURN                                         

End of function test

Function baz:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YS6Ze
function name:  baz
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   NEW                                              $0      'RewindableGenerator'
          1        SEND_VAL_EX                                              'test'
          2        DO_FCALL                                      0          
          3      > RETURN                                                   $0
   50     4*     > RETURN                                                   null

End of function baz

Class RewindableGenerator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YS6Ze
function name:  __construct
number of ops:  6
compiled vars:  !0 = $iteratorConstructor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        ASSIGN_OBJ                                               'iteratorConstructor'
          2        OP_DATA                                                  !0
   11     3        INIT_METHOD_CALL                                         'rewind'
          4        DO_FCALL                                      0          
   12     5      > RETURN                                                   null

End of function __construct

Function rewind:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/YS6Ze
function name:  rewind
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_OBJ_R                                      ~0      'rewound'
          1        BOOL_NOT                                         ~1      ~0
          2      > JMPZ                                                     ~1, ->10
   16     3    >   FETCH_OBJ_R                                      ~3      'iteratorConstructor'
          4        INIT_DYNAMIC_CALL                                        ~3
          5        DO_FCALL                                      0  $4      
          6        ASSIGN_OBJ                                               'iterator'
          7        OP_DATA                                                  $4
   17     8        ASSIGN_OBJ                                               'rewound'
          9        OP_DATA                                                  <true>
   19    10    > > RETURN                                                   null

End of function rewind

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YS6Ze
function name:  next
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   FETCH_OBJ_R                                      ~0      'iterator'
          1        INIT_METHOD_CALL                                         ~0, 'next'
          2        DO_FCALL                                      0          
   23     3        ASSIGN_OBJ                                               'rewound'
          4        OP_DATA                                                  <false>
   24     5      > RETURN                                                   null

End of function next

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

End of function valid

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

End of function key

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

End of function current

End of class RewindableGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.98 ms | 1411 KiB | 14 Q