3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_to_anon_object($arr): object { $obj = new class {}; foreach ( $arr as $key => $value ) { $obj->{$key} = $value; } return $obj; } class EmptyClass {} function array_to_normal_object($arr, $className): object { $obj = new class {}; foreach ( $arr as $key => $value ) { $obj->{$key} = $value; } return $obj; } var_dump( json_encode([1,2,3]) ); var_dump( json_encode((object)[1,2,3]) ); var_dump( json_encode(array_to_anon_object([1,2,3])) ); var_dump( json_encode(array_to_normal_object([1,2,3], EmptyClass::class)) ); var_dump( json_encode(array_to_normal_object([1,2,3], stdClass::class)) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fGRBj
function name:  (null)
number of ops:  43
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'json_encode'
          2        SEND_VAL                                                 <array>
          3        DO_ICALL                                         $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   22     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'json_encode'
          8        CAST                                          8  ~2      <array>
          9        SEND_VAL                                                 ~2
         10        DO_ICALL                                         $3      
         11        SEND_VAR                                                 $3
         12        DO_ICALL                                                 
   23    13        INIT_FCALL                                               'var_dump'
         14        INIT_FCALL                                               'json_encode'
         15        INIT_FCALL                                               'array_to_anon_object'
         16        SEND_VAL                                                 <array>
         17        DO_FCALL                                      0  $5      
         18        SEND_VAR                                                 $5
         19        DO_ICALL                                         $6      
         20        SEND_VAR                                                 $6
         21        DO_ICALL                                                 
   24    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'json_encode'
         24        INIT_FCALL                                               'array_to_normal_object'
         25        SEND_VAL                                                 <array>
         26        SEND_VAL                                                 'EmptyClass'
         27        DO_FCALL                                      0  $8      
         28        SEND_VAR                                                 $8
         29        DO_ICALL                                         $9      
         30        SEND_VAR                                                 $9
         31        DO_ICALL                                                 
   25    32        INIT_FCALL                                               'var_dump'
         33        INIT_FCALL                                               'json_encode'
         34        INIT_FCALL                                               'array_to_normal_object'
         35        SEND_VAL                                                 <array>
         36        SEND_VAL                                                 'stdClass'
         37        DO_FCALL                                      0  $11     
         38        SEND_VAR                                                 $11
         39        DO_ICALL                                         $12     
         40        SEND_VAR                                                 $12
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Function array_to_anon_object:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/fGRBj
function name:  array_to_anon_object
number of ops:  16
compiled vars:  !0 = $arr, !1 = $obj, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        DECLARE_ANON_CLASS                               1.02766e-321    
          2        NEW                                              $5      $4
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $5
    5     5      > FE_RESET_R                                       $8      !0, ->11
          6    > > FE_FETCH_R                                       ~9      $8, !2, ->11
          7    >   ASSIGN                                                   !3, ~9
    6     8        ASSIGN_OBJ                                               !1, !3
          9        OP_DATA                                                  !2
    5    10      > JMP                                                      ->6
         11    >   FE_FREE                                                  $8
    8    12        VERIFY_RETURN_TYPE                                       !1
         13      > RETURN                                                   !1
    9    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function array_to_anon_object

Function array_to_normal_object:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/fGRBj
function name:  array_to_normal_object
number of ops:  17
compiled vars:  !0 = $arr, !1 = $className, !2 = $obj, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        DECLARE_ANON_CLASS                               <unknown> 
          3        NEW                                              $6      $5
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $6
   15     6      > FE_RESET_R                                       $9      !0, ->12
          7    > > FE_FETCH_R                                       ~10     $9, !3, ->12
          8    >   ASSIGN                                                   !4, ~10
   16     9        ASSIGN_OBJ                                               !2, !4
         10        OP_DATA                                                  !3
   15    11      > JMP                                                      ->7
         12    >   FE_FREE                                                  $9
   18    13        VERIFY_RETURN_TYPE                                       !2
         14      > RETURN                                                   !2
   19    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function array_to_normal_object

Class class@anonymous: [no user functions]
Class EmptyClass: [no user functions]
Class class@anonymous: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.62 ms | 1407 KiB | 20 Q