3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cast($source, $class) { try { $sourceReflection = new ReflectionClass($source); $targetReflection = new ReflectionClass($class); $targetProperties = $targetReflection->getProperties(); $target = $targetReflection->newInstanceWithoutConstructor(); foreach ($targetProperties as $targetProperty) { $name = $targetProperty->getName(); if ($sourceReflection->hasProperty($name)) { $sourceProperty = $sourceReflection->getProperty($name); $sourceProperty->setAccessible(true); $targetProperty->setAccessible(true); $targetProperty->setValue($target, $sourceProperty->getValue($source)); } } return $target; } catch (ReflectionException $reflectionException) { return null; } } class Foo { private $a = __CLASS__; private $b = __CLASS__; private $c = __CLASS__; } class Bar { private $b = __CLASS__; private $c = __CLASS__; private $d = __CLASS__; } $foo = new Foo(); $bar = cast($foo, 'Bar'); var_dump($bar);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/05WPe
function name:  (null)
number of ops:  12
compiled vars:  !0 = $foo, !1 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $2      'Foo'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   48     3        INIT_FCALL                                               'cast'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 'Bar'
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !1, $5
   49     8        INIT_FCALL                                               'var_dump'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Function cast:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 43
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 43
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 42
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 42
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Found catch point at position: 46
Branch analysis from position: 46
2 jumps found. (Code = 107) Position 1 = 47, Position 2 = -2
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/05WPe
function name:  cast
number of ops:  49
compiled vars:  !0 = $source, !1 = $class, !2 = $sourceReflection, !3 = $targetReflection, !4 = $targetProperties, !5 = $target, !6 = $targetProperty, !7 = $name, !8 = $sourceProperty, !9 = $reflectionException
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        NEW                                              $10     'ReflectionClass'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $10
    8     6        NEW                                              $13     'ReflectionClass'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !3, $13
    9    10        INIT_METHOD_CALL                                         !3, 'getProperties'
         11        DO_FCALL                                      0  $16     
         12        ASSIGN                                                   !4, $16
   10    13        INIT_METHOD_CALL                                         !3, 'newInstanceWithoutConstructor'
         14        DO_FCALL                                      0  $18     
         15        ASSIGN                                                   !5, $18
   11    16      > FE_RESET_R                                       $20     !4, ->43
         17    > > FE_FETCH_R                                               $20, !6, ->43
   13    18    >   INIT_METHOD_CALL                                         !6, 'getName'
         19        DO_FCALL                                      0  $21     
         20        ASSIGN                                                   !7, $21
   14    21        INIT_METHOD_CALL                                         !2, 'hasProperty'
         22        SEND_VAR_EX                                              !7
         23        DO_FCALL                                      0  $23     
         24      > JMPZ                                                     $23, ->42
   16    25    >   INIT_METHOD_CALL                                         !2, 'getProperty'
         26        SEND_VAR_EX                                              !7
         27        DO_FCALL                                      0  $24     
         28        ASSIGN                                                   !8, $24
   17    29        INIT_METHOD_CALL                                         !8, 'setAccessible'
         30        SEND_VAL_EX                                              <true>
         31        DO_FCALL                                      0          
   18    32        INIT_METHOD_CALL                                         !6, 'setAccessible'
         33        SEND_VAL_EX                                              <true>
         34        DO_FCALL                                      0          
   19    35        INIT_METHOD_CALL                                         !6, 'setValue'
         36        SEND_VAR_EX                                              !5
         37        INIT_METHOD_CALL                                         !8, 'getValue'
         38        SEND_VAR_EX                                              !0
         39        DO_FCALL                                      0  $28     
         40        SEND_VAR_NO_REF_EX                                       $28
         41        DO_FCALL                                      0          
   11    42    > > JMP                                                      ->17
         43    >   FE_FREE                                                  $20
   22    44      > RETURN                                                   !5
         45*       JMP                                                      ->48
   23    46  E > > CATCH                                       last         'ReflectionException'
   25    47    > > RETURN                                                   null
   27    48*     > RETURN                                                   null

End of function cast

Class Foo: [no user functions]
Class Bar: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.39 ms | 1403 KiB | 16 Q