3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Enumerator; use \ArrayIterator; use \Traversable; class Enumerable { /** * * @var Traversable */ private $traversable; /** * * @param Traversable|array $traversable * @return Enumerable */ public static function enumerate($traversable) { return new Enumerable($traversable); } /** * * @param Traversable|array $traversable */ public function __construct($traversable) { if ($traversable instanceof Traversable) { $this->traversable = $traversable; } else { $this->traversable = new ArrayIterator((array) $traversable); } } /** * * @param callable $callable * @return Enumerable */ public function select(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { yield $key => $callable($value, $key); } }; return new Enumerable($generator()); } /** * * @param callable $callable * @return Enumerable */ public function filter(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { if ($callable($value, $key)) { yield $key => $value; } } }; return new Enumerable($generator()); } /** * * @return array */ public function toArray() { return iterator_to_array($this->traversable); } } $array = Enumerable::enumerate([ 1, 6, 3, 8, 6, 1, 9, 4, 2, 4, 6, 4, 2, ])->select(function ($i) { echo sprintf('Selecting %d * 2', $i) . PHP_EOL; return $i * 2; })->filter(function ($i) { echo sprintf(' Filtering %d > 10', $i) . PHP_EOL; return $i > 10; })->select(function ($i) { echo sprintf(' Selecting %d * 3', $i) . PHP_EOL; return $i * 3; })->filter(function ($i) { echo sprintf(' Filtering %d > 40', $i) . PHP_EOL; return $i > 40; })->select(function ($i) { echo sprintf(' Selecting %d * 4', $i) . PHP_EOL; return $i * 3; })->toArray(); var_dump($array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  (null)
number of ops:  30
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   INIT_STATIC_METHOD_CALL                                  'Enumerator%5CEnumerable', 'enumerate'
   85     1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $1      
   86     3        INIT_METHOD_CALL                                         $1, 'select'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A86%242'
   89     5        SEND_VAL_EX                                              ~2
          6        DO_FCALL                                      0  $3      
          7        INIT_METHOD_CALL                                         $3, 'filter'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A89%243'
   92     9        SEND_VAL_EX                                              ~4
         10        DO_FCALL                                      0  $5      
         11        INIT_METHOD_CALL                                         $5, 'select'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A92%244'
   95    13        SEND_VAL_EX                                              ~6
         14        DO_FCALL                                      0  $7      
         15        INIT_METHOD_CALL                                         $7, 'filter'
         16        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A95%245'
   98    17        SEND_VAL_EX                                              ~8
         18        DO_FCALL                                      0  $9      
         19        INIT_METHOD_CALL                                         $9, 'select'
         20        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A98%246'
  101    21        SEND_VAL_EX                                              ~10
         22        DO_FCALL                                      0  $11     
         23        INIT_METHOD_CALL                                         $11, 'toArray'
         24        DO_FCALL                                      0  $12     
   84    25        ASSIGN                                                   !0, $12
  103    26        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Cvar_dump'
         27        SEND_VAR_EX                                              !0
         28        DO_FCALL                                      0          
         29      > RETURN                                                   1

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A48%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  15
compiled vars:  !0 = $callable, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   GENERATOR_CREATE                                         
          1        BIND_STATIC                                              !0
   49     2        FETCH_THIS                                       $3      
          3        FETCH_OBJ_R                                      ~4      $3, 'traversable'
          4      > FE_RESET_R                                       $5      ~4, ->13
          5    > > FE_FETCH_R                                       ~6      $5, !1, ->13
          6    >   ASSIGN                                                   !2, ~6
   50     7        INIT_DYNAMIC_CALL                                        !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $8      
         11        YIELD                                                    $8, !2
   49    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $5
   52    14      > GENERATOR_RETURN                                         

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A48%240

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A63%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
Branch analysis from position: 14
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 14
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  16
compiled vars:  !0 = $callable, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   GENERATOR_CREATE                                         
          1        BIND_STATIC                                              !0
   64     2        FETCH_THIS                                       $3      
          3        FETCH_OBJ_R                                      ~4      $3, 'traversable'
          4      > FE_RESET_R                                       $5      ~4, ->14
          5    > > FE_FETCH_R                                       ~6      $5, !1, ->14
          6    >   ASSIGN                                                   !2, ~6
   65     7        INIT_DYNAMIC_CALL                                        !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $8      
         11      > JMPZ                                                     $8, ->13
   66    12    >   YIELD                                                    !1, !2
   64    13    > > JMP                                                      ->5
         14    >   FE_FREE                                                  $5
   69    15      > GENERATOR_RETURN                                         

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A63%241

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A86%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  11
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   87     1        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Csprintf'
          2        SEND_VAL_EX                                              'Selecting+%25d+%2A++2'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        FETCH_CONSTANT                                   ~2      'Enumerator%5CPHP_EOL'
          6        CONCAT                                           ~3      $1, ~2
          7        ECHO                                                     ~3
   88     8        MUL                                              ~4      !0, 2
          9      > RETURN                                                   ~4
   89    10*     > RETURN                                                   null

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A86%242

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A89%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  11
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   90     1        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Csprintf'
          2        SEND_VAL_EX                                              '++Filtering+%25d+%3E+10'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        FETCH_CONSTANT                                   ~2      'Enumerator%5CPHP_EOL'
          6        CONCAT                                           ~3      $1, ~2
          7        ECHO                                                     ~3
   91     8        IS_SMALLER                                       ~4      10, !0
          9      > RETURN                                                   ~4
   92    10*     > RETURN                                                   null

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A89%243

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A92%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  11
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   93     1        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Csprintf'
          2        SEND_VAL_EX                                              '++++Selecting+%25d+%2A+3'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        FETCH_CONSTANT                                   ~2      'Enumerator%5CPHP_EOL'
          6        CONCAT                                           ~3      $1, ~2
          7        ECHO                                                     ~3
   94     8        MUL                                              ~4      !0, 3
          9      > RETURN                                                   ~4
   95    10*     > RETURN                                                   null

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A92%244

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A95%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  11
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   96     1        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Csprintf'
          2        SEND_VAL_EX                                              '++++++Filtering+%25d+%3E+40'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        FETCH_CONSTANT                                   ~2      'Enumerator%5CPHP_EOL'
          6        CONCAT                                           ~3      $1, ~2
          7        ECHO                                                     ~3
   97     8        IS_SMALLER                                       ~4      40, !0
          9      > RETURN                                                   ~4
   98    10*     > RETURN                                                   null

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A95%245

Function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A98%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  Enumerator\{closure}
number of ops:  11
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   99     1        INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Csprintf'
          2        SEND_VAL_EX                                              '++++++++Selecting+%25d+%2A+4'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        FETCH_CONSTANT                                   ~2      'Enumerator%5CPHP_EOL'
          6        CONCAT                                           ~3      $1, ~2
          7        ECHO                                                     ~3
  100     8        MUL                                              ~4      !0, 3
          9      > RETURN                                                   ~4
  101    10*     > RETURN                                                   null

End of function %00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A98%246

Class Enumerator\Enumerable:
Function enumerate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  enumerate
number of ops:  6
compiled vars:  !0 = $traversable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        NEW                                              $1      'Enumerator%5CEnumerable'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   25     5*     > RETURN                                                   null

End of function enumerate

Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  __construct
number of ops:  13
compiled vars:  !0 = $traversable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        INSTANCEOF                                               !0, 'Traversable'
          2      > JMPZ                                                     ~1, ->6
   34     3    >   ASSIGN_OBJ                                               'traversable'
          4        OP_DATA                                                  !0
          5      > JMP                                                      ->12
   37     6    >   NEW                                              $4      'ArrayIterator'
          7        CAST                                          7  ~5      !0
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0          
         10        ASSIGN_OBJ                                               'traversable'
         11        OP_DATA                                                  $4
   39    12    > > RETURN                                                   null

End of function __construct

Function select:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  select
number of ops:  11
compiled vars:  !0 = $callable, !1 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     1        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A48%240'
          2        BIND_LEXICAL                                             ~2, !0
          3        ASSIGN                                                   !1, ~2
   53     4        NEW                                              $4      'Enumerator%5CEnumerable'
          5        INIT_DYNAMIC_CALL                                        !1
          6        DO_FCALL                                      0  $5      
          7        SEND_VAR_NO_REF_EX                                       $5
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $4
   54    10*     > RETURN                                                   null

End of function select

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  filter
number of ops:  11
compiled vars:  !0 = $callable, !1 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        DECLARE_LAMBDA_FUNCTION                                  '%00enumerator%5C%7Bclosure%7D%2Fin%2FI30Sb%3A63%241'
          2        BIND_LEXICAL                                             ~2, !0
          3        ASSIGN                                                   !1, ~2
   70     4        NEW                                              $4      'Enumerator%5CEnumerable'
          5        INIT_DYNAMIC_CALL                                        !1
          6        DO_FCALL                                      0  $5      
          7        SEND_VAR_NO_REF_EX                                       $5
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $4
   71    10*     > RETURN                                                   null

End of function filter

Function toarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I30Sb
function name:  toArray
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   INIT_NS_FCALL_BY_NAME                                    'Enumerator%5Citerator_to_array'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $0      'traversable'
          3        SEND_FUNC_ARG                                            $0
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
   80     6*     > RETURN                                                   null

End of function toarray

End of class Enumerator\Enumerable.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.2 ms | 1416 KiB | 19 Q