3v4l.org

run code in 500+ PHP versions simultaneously
<?php interface ArrayKeyable { public function toArrayKey(): string|int; } final class MyObject implements ArrayKeyable { public function __construct( public readonly int $id, public readonly string $name, ) {} public function toArrayKey(): string { return $this->name; } } $objects = []; $data = [[1, 'Jane'], [2, 'John'], [3, 'Jake'], [4, 'Jane']]; foreach($data as [$id, $name]) { $object = new MyObject($id, $name); $objects[$object->toArrayKey()][] = $object; // would turn into $objects[$object][] = $object; } var_dump($objects);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/Vi8kY
function name:  (null)
number of ops:  26
compiled vars:  !0 = $objects, !1 = $data, !2 = $id, !3 = $name, !4 = $object
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   DECLARE_CLASS                                                'myobject'
   19     1        ASSIGN                                                       !0, <array>
   20     2        ASSIGN                                                       !1, <array>
   21     3      > FE_RESET_R                                           $7      !1, ->21
          4    > > FE_FETCH_R                                                   $7, $8, ->21
          5    >   FETCH_LIST_R                                         $9      $8, 0
          6        ASSIGN                                                       !2, $9
          7        FETCH_LIST_R                                         $11     $8, 1
          8        ASSIGN                                                       !3, $11
          9        FREE                                                         $8
   22    10        NEW                                                  $13     'MyObject'
         11        SEND_VAR_EX                                                  !2
         12        SEND_VAR_EX                                                  !3
         13        DO_FCALL                                          0          
         14        ASSIGN                                                       !4, $13
   23    15        INIT_METHOD_CALL                                             !4, 'toArrayKey'
         16        DO_FCALL                                          0  $16     
         17        FETCH_DIM_W                                          $17     !0, $16
         18        ASSIGN_DIM                                                   $17
         19        OP_DATA                                                      !4
   21    20      > JMP                                                          ->4
         21    >   FE_FREE                                                      $7
   27    22        INIT_FCALL                                                   'var_dump'
         23        SEND_VAR                                                     !0
         24        DO_ICALL                                                     
         25      > RETURN                                                       1

Class ArrayKeyable:
Function toarraykey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Vi8kY
function name:  toArrayKey
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   VERIFY_RETURN_TYPE                                           
          1      > RETURN                                                       null

End of function toarraykey

End of class ArrayKeyable.

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

End of function __construct

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

End of function toarraykey

End of class MyObject.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
155.78 ms | 1385 KiB | 14 Q