3v4l.org

run code in 500+ PHP versions simultaneously
<?php $fruitpacks = [ ['apples', 3], ['pears', 1], ['apples', 6], ['grapes', 22], ]; final class Basket { private $items; private function __construct(array $items) { $this->items = $items; } public function add(string $name, int $quantity) : self { return new self([$name => $quantity + ($this->items[$name] ?? 0)] + $this->items); } public function asArray() : array { return $this->items; } public static function empty() : self { return new self([]); } public static function addToBasket(self $basket, array $item) : self { return $basket->add(...$item); } } var_dump(array_reduce($fruitpacks, [Basket::class, 'addToBasket'], Basket::empty())->asArray());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  (null)
number of ops:  14
compiled vars:  !0 = $fruitpacks
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   28     1        INIT_FCALL                                                   'var_dump'
          2        INIT_FCALL                                                   'array_reduce'
          3        SEND_VAR                                                     !0
          4        SEND_VAL                                                     <array>
          5        INIT_STATIC_METHOD_CALL                                      'Basket', 'empty'
          6        DO_FCALL                                          0  $2      
          7        SEND_VAR                                                     $2
          8        DO_ICALL                                             $3      
          9        INIT_METHOD_CALL                                             $3, 'asArray'
         10        DO_FCALL                                          0  $4      
         11        SEND_VAR                                                     $4
         12        DO_ICALL                                                     
   29    13      > RETURN                                                       1

Class Basket:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  __construct
number of ops:  4
compiled vars:  !0 = $items
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'items'
          2        OP_DATA                                                      !0
          3      > RETURN                                                       null

End of function __construct

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  add
number of ops:  17
compiled vars:  !0 = $name, !1 = $quantity
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   16     2        NEW                              self                $2      
          3        FETCH_OBJ_IS                                         ~3      'items'
          4        FETCH_DIM_IS                                         ~4      ~3, !0
          5        COALESCE                                             ~5      ~4
          6        QM_ASSIGN                                            ~5      0
          7        ADD                                                  ~6      !1, ~5
          8        INIT_ARRAY                                           ~7      ~6, !0
          9        FETCH_OBJ_R                                          ~8      'items'
         10        ADD                                                  ~9      ~7, ~8
         11        SEND_VAL_EX                                                  ~9
         12        DO_FCALL                                          0          
         13        VERIFY_RETURN_TYPE                                           $2
         14      > RETURN                                                       $2
   17    15*       VERIFY_RETURN_TYPE                                           
         16*     > RETURN                                                       null

End of function add

Function asarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  asArray
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   FETCH_OBJ_R                                          ~0      'items'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
          3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function asarray

Function empty:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  empty
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   NEW                              self                $0      
          1        SEND_VAL_EX                                                  <array>
          2        DO_FCALL                                          0          
          3        VERIFY_RETURN_TYPE                                           $0
          4      > RETURN                                                       $0
          5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function empty

Function addtobasket:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Sh04
function name:  addToBasket
number of ops:  10
compiled vars:  !0 = $basket, !1 = $item
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   24     2        INIT_METHOD_CALL                                             !0, 'add'
          3        SEND_UNPACK                                                  !1
          4        CHECK_UNDEF_ARGS                                             
          5        DO_FCALL                                          1  $2      
          6        VERIFY_RETURN_TYPE                                           $2
          7      > RETURN                                                       $2
   25     8*       VERIFY_RETURN_TYPE                                           
          9*     > RETURN                                                       null

End of function addtobasket

End of class Basket.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
186.92 ms | 2207 KiB | 15 Q