3v4l.org

run code in 300+ PHP versions simultaneously
<?php readonly class Address { public function __construct( public string $street, public string $city, public string $state, public string $country, public string $postalCode, ){ } } readonly class Person { public function __construct( public string $name, public int $age, public Address $address, ){ } } $bob = new Person('Bob', 32, new Address('123 Main St', 'Testington', 'Test', 'Test', '12345')); $bobJson = json_encode($bob); $decodedBob = json_decode_class($bobJson, Person::class); if ($bob == $decodedBob){ echo 'These are the same person!', PHP_EOL; } else { echo 'We have different people here!', PHP_EOL; } function json_decode_class(string $json, string $class){ return construct_object(json_decode($json), $class); } function construct_object(stdClass $data, string $class){ $rc = new ReflectionClass($class); $properties = $rc->getConstructor()->getParameters(); $constructorArgs = []; foreach ($properties as $prop){ $type = $prop->getType(); if (property_exists($data, $prop->getName())){ $value = $data->{$prop->getName()}; if (!$type->isBuiltin()){ $value = construct_object($value, $type->getName()); } $constructorArgs[$prop->getName()] = $value; } } return $rc->newInstanceArgs($constructorArgs); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 27
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KsOIA
function name:  (null)
number of ops:  30
compiled vars:  !0 = $bob, !1 = $bobJson, !2 = $decodedBob
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   NEW                                              $3      'Person'
          1        SEND_VAL_EX                                              'Bob'
          2        SEND_VAL_EX                                              32
          3        NEW                                              $4      'Address'
          4        SEND_VAL_EX                                              '123+Main+St'
          5        SEND_VAL_EX                                              'Testington'
          6        SEND_VAL_EX                                              'Test'
          7        SEND_VAL_EX                                              'Test'
          8        SEND_VAL_EX                                              '12345'
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $4
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !0, $3
   24    13        INIT_FCALL                                               'json_encode'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $8      
         16        ASSIGN                                                   !1, $8
   25    17        INIT_FCALL_BY_NAME                                       'json_decode_class'
         18        SEND_VAR_EX                                              !1
         19        SEND_VAL_EX                                              'Person'
         20        DO_FCALL                                      0  $10     
         21        ASSIGN                                                   !2, $10
   27    22        IS_EQUAL                                                 !0, !2
         23      > JMPZ                                                     ~12, ->27
   28    24    >   ECHO                                                     'These+are+the+same+person%21'
         25        ECHO                                                     '%0A'
   27    26      > JMP                                                      ->29
   30    27    >   ECHO                                                     'We+have+different+people+here%21'
         28        ECHO                                                     '%0A'
   53    29    > > RETURN                                                   1

Function json_decode_class:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KsOIA
function name:  json_decode_class
number of ops:  11
compiled vars:  !0 = $json, !1 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        INIT_FCALL_BY_NAME                                       'construct_object'
          3        INIT_FCALL                                               'json_decode'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR_NO_REF_EX                                       $2
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   35    10*     > RETURN                                                   null

End of function json_decode_class

Function construct_object:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 44
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 44
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 43
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 39
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 39
Branch analysis from position: 43
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/KsOIA
function name:  construct_object
number of ops:  50
compiled vars:  !0 = $data, !1 = $class, !2 = $rc, !3 = $properties, !4 = $constructorArgs, !5 = $prop, !6 = $type, !7 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   38     2        NEW                                              $8      'ReflectionClass'
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $8
   39     6        INIT_METHOD_CALL                                         !2, 'getConstructor'
          7        DO_FCALL                                      0  $11     
          8        INIT_METHOD_CALL                                         $11, 'getParameters'
          9        DO_FCALL                                      0  $12     
         10        ASSIGN                                                   !3, $12
   40    11        ASSIGN                                                   !4, <array>
   41    12      > FE_RESET_R                                       $15     !3, ->44
         13    > > FE_FETCH_R                                               $15, !5, ->44
   42    14    >   INIT_METHOD_CALL                                         !5, 'getType'
         15        DO_FCALL                                      0  $16     
         16        ASSIGN                                                   !6, $16
   43    17        INIT_FCALL                                               'property_exists'
         18        SEND_VAR                                                 !0
         19        INIT_METHOD_CALL                                         !5, 'getName'
         20        DO_FCALL                                      0  $18     
         21        SEND_VAR                                                 $18
         22        DO_ICALL                                         $19     
         23      > JMPZ                                                     $19, ->43
   44    24    >   INIT_METHOD_CALL                                         !5, 'getName'
         25        DO_FCALL                                      0  $20     
         26        FETCH_OBJ_R                                      ~21     !0, $20
         27        ASSIGN                                                   !7, ~21
   45    28        INIT_METHOD_CALL                                         !6, 'isBuiltin'
         29        DO_FCALL                                      0  $23     
         30        BOOL_NOT                                         ~24     $23
         31      > JMPZ                                                     ~24, ->39
   46    32    >   INIT_FCALL_BY_NAME                                       'construct_object'
         33        SEND_VAR_EX                                              !7
         34        INIT_METHOD_CALL                                         !6, 'getName'
         35        DO_FCALL                                      0  $25     
         36        SEND_VAR_NO_REF_EX                                       $25
         37        DO_FCALL                                      0  $26     
         38        ASSIGN                                                   !7, $26
   48    39    >   INIT_METHOD_CALL                                         !5, 'getName'
         40        DO_FCALL                                      0  $28     
         41        ASSIGN_DIM                                               !4, $28
         42        OP_DATA                                                  !7
   41    43    > > JMP                                                      ->13
         44    >   FE_FREE                                                  $15
   52    45        INIT_METHOD_CALL                                         !2, 'newInstanceArgs'
         46        SEND_VAR_EX                                              !4
         47        DO_FCALL                                      0  $30     
         48      > RETURN                                                   $30
   53    49*     > RETURN                                                   null

End of function construct_object

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

End of function __construct

End of class Address.

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

End of function __construct

End of class Person.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.75 ms | 1447 KiB | 16 Q