3v4l.org

run code in 300+ PHP versions simultaneously
<?php $names = ['Alice', 'Bob', 'Charlie', 'Alice', 'Dave', 'Alice']; class Name { public $name; public $id; public function __construct($name, $id = 0) { $this->name = $name; $this->id = $id; } } function is_similar($a, $b) { return $a->name == $b->name; } function contains(array $list, $item, callable $comparitor) { foreach ($list as $test) { if ($comparitor) { return true; } } return false; } function partial($func, $arg1) { $func_args = func_get_args(); $args = array_slice($func_args, 1); return function() use($func, $args) { $full_args = array_merge($args, func_get_args()); return call_user_func_array($func, $full_args); }; } // Transform strings -> objects $mappedNames = array_map(function($name) { return new Name($name); }, $names); // Deduplicate list $reduction = function(callable $comparitor, array $carry, $item) { if (contains($carry, $item, $comparitor)) { return $carry; } return array_merge($carry, array($item)); }; $reducedNames = array_reduce($mappedNames, partial($reduction, "is_similar"), []); $idNames = array_map(function($item, $id) { return new Name($item->name, $id); }, $reducedNames, range(1, count($reducedNames))); var_dump($idNames);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  (null)
number of ops:  35
compiled vars:  !0 = $names, !1 = $mappedNames, !2 = $reduction, !3 = $reducedNames, !4 = $idNames
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   38     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FLZu5q%3A38%241'
          3        SEND_VAL                                                 ~6
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $7      
          6        ASSIGN                                                   !1, $7
   41     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FLZu5q%3A41%242'
          8        ASSIGN                                                   !2, ~9
   48     9        INIT_FCALL                                               'array_reduce'
         10        SEND_VAR                                                 !1
         11        INIT_FCALL                                               'partial'
         12        SEND_VAR                                                 !2
         13        SEND_VAL                                                 'is_similar'
         14        DO_FCALL                                      0  $11     
         15        SEND_VAR                                                 $11
         16        SEND_VAL                                                 <array>
         17        DO_ICALL                                         $12     
         18        ASSIGN                                                   !3, $12
   50    19        INIT_FCALL                                               'array_map'
         20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FLZu5q%3A50%243'
   52    21        SEND_VAL                                                 ~14
         22        SEND_VAR                                                 !3
         23        INIT_FCALL                                               'range'
         24        SEND_VAL                                                 1
         25        COUNT                                            ~15     !3
         26        SEND_VAL                                                 ~15
         27        DO_ICALL                                         $16     
         28        SEND_VAR                                                 $16
         29        DO_ICALL                                         $17     
   50    30        ASSIGN                                                   !4, $17
   54    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !4
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

Function is_similar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  is_similar
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        FETCH_OBJ_R                                      ~2      !0, 'name'
          3        FETCH_OBJ_R                                      ~3      !1, 'name'
          4        IS_EQUAL                                         ~4      ~2, ~3
          5      > RETURN                                                   ~4
   16     6*     > RETURN                                                   null

End of function is_similar

Function contains:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/LZu5q
function name:  contains
number of ops:  12
compiled vars:  !0 = $list, !1 = $item, !2 = $comparitor, !3 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   19     3      > FE_RESET_R                                       $4      !0, ->9
          4    > > FE_FETCH_R                                               $4, !3, ->9
   20     5    > > JMPZ                                                     !2, ->8
   21     6    >   FE_FREE                                                  $4
          7      > RETURN                                                   <true>
   19     8    > > JMP                                                      ->4
          9    >   FE_FREE                                                  $4
   24    10      > RETURN                                                   <false>
   25    11*     > RETURN                                                   null

End of function contains

Function partial:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  partial
number of ops:  14
compiled vars:  !0 = $func, !1 = $arg1, !2 = $func_args, !3 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        FUNC_GET_ARGS                                    ~4      
          3        ASSIGN                                                   !2, ~4
   29     4        INIT_FCALL                                               'array_slice'
          5        SEND_VAR                                                 !2
          6        SEND_VAL                                                 1
          7        DO_ICALL                                         $6      
          8        ASSIGN                                                   !3, $6
   31     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FLZu5q%3A31%240'
         10        BIND_LEXICAL                                             ~8, !0
         11        BIND_LEXICAL                                             ~8, !3
   34    12      > RETURN                                                   ~8
   35    13*     > RETURN                                                   null

End of function partial

Function %00%7Bclosure%7D%2Fin%2FLZu5q%3A31%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $func, !1 = $args, !2 = $full_args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   32     2        INIT_FCALL                                               'array_merge'
          3        SEND_VAR                                                 !1
          4        FUNC_GET_ARGS                                    ~3      
          5        SEND_VAL                                                 ~3
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !2, $4
   33     8        INIT_USER_CALL                                0          'call_user_func_array', !0
          9        SEND_ARRAY                                               !2
         10        CHECK_UNDEF_ARGS                                         
         11        DO_FCALL                                      0  $6      
         12      > RETURN                                                   $6
   34    13*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FLZu5q%3A31%240

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

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

Function %00%7Bclosure%7D%2Fin%2FLZu5q%3A41%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  {closure}
number of ops:  17
compiled vars:  !0 = $comparitor, !1 = $carry, !2 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   42     3        INIT_FCALL                                               'contains'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $3      
          8      > JMPZ                                                     $3, ->10
   43     9    > > RETURN                                                   !1
   45    10    >   INIT_FCALL                                               'array_merge'
         11        SEND_VAR                                                 !1
         12        INIT_ARRAY                                       ~4      !2
         13        SEND_VAL                                                 ~4
         14        DO_ICALL                                         $5      
         15      > RETURN                                                   $5
   46    16*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FLZu5q%3A41%242

Function %00%7Bclosure%7D%2Fin%2FLZu5q%3A50%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LZu5q
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $item, !1 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   51     2        NEW                                              $2      'Name'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $3      !0, 'name'
          5        SEND_FUNC_ARG                                            $3
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
          8      > RETURN                                                   $2
   52     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FLZu5q%3A50%243

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

End of function __construct

End of class Name.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.03 ms | 1411 KiB | 27 Q