3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class ObjectCreator { public static function create(string $className, array $properties) { $instance = (new ReflectionClass($className)) ->newInstanceWithoutConstructor(); $reflection = new ReflectionObject($instance); foreach ($properties as $field => $value) { $property = $reflection->getProperty($field); $property->setAccessible(true); $property->setValue($instance, $value); } return $instance; } } final class Orang { /** * @var string */ private $id; public function getId(): string { return $this->id; } /** * @var string */ private $nama; public function getNama(): string { return $this->nama; } public function __construct(string $id, string $nama) { $this->id = $id; $this->nama = $nama; } } $orang = ObjectCreator::create(Orang::class, [ 'id' => 'some_id', 'nama' => 'Some Name' ]); echo $orang->getId(), PHP_EOL; echo $orang->getNama(), PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9W8K2
function name:  (null)
number of ops:  14
compiled vars:  !0 = $orang
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_STATIC_METHOD_CALL                                  'ObjectCreator', 'create'
          1        SEND_VAL                                                 'Orang'
   52     2        SEND_VAL                                                 <array>
   51     3        DO_FCALL                                      0  $1      
          4        ASSIGN                                                   !0, $1
   56     5        INIT_METHOD_CALL                                         !0, 'getId'
          6        DO_FCALL                                      0  $3      
          7        ECHO                                                     $3
          8        ECHO                                                     '%0A'
   57     9        INIT_METHOD_CALL                                         !0, 'getNama'
         10        DO_FCALL                                      0  $4      
         11        ECHO                                                     $4
         12        ECHO                                                     '%0A'
         13      > RETURN                                                   1

Class ObjectCreator:
Function create:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 27
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 27
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/9W8K2
function name:  create
number of ops:  30
compiled vars:  !0 = $className, !1 = $properties, !2 = $instance, !3 = $reflection, !4 = $value, !5 = $field, !6 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        NEW                                              $7      'ReflectionClass'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
    8     5        INIT_METHOD_CALL                                         $7, 'newInstanceWithoutConstructor'
          6        DO_FCALL                                      0  $9      
    7     7        ASSIGN                                                   !2, $9
   10     8        NEW                                              $11     'ReflectionObject'
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !3, $11
   12    12      > FE_RESET_R                                       $14     !1, ->27
         13    > > FE_FETCH_R                                       ~15     $14, !4, ->27
         14    >   ASSIGN                                                   !5, ~15
   13    15        INIT_METHOD_CALL                                         !3, 'getProperty'
         16        SEND_VAR_EX                                              !5
         17        DO_FCALL                                      0  $17     
         18        ASSIGN                                                   !6, $17
   14    19        INIT_METHOD_CALL                                         !6, 'setAccessible'
         20        SEND_VAL_EX                                              <true>
         21        DO_FCALL                                      0          
   15    22        INIT_METHOD_CALL                                         !6, 'setValue'
         23        SEND_VAR_EX                                              !2
         24        SEND_VAR_EX                                              !4
         25        DO_FCALL                                      0          
   12    26      > JMP                                                      ->13
         27    >   FE_FREE                                                  $14
   18    28      > RETURN                                                   !2
   19    29*     > RETURN                                                   null

End of function create

End of class ObjectCreator.

Class Orang:
Function getid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9W8K2
function name:  getId
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   FETCH_OBJ_R                                      ~0      'id'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   32     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getid

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

End of function getnama

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

End of function __construct

End of class Orang.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
276.19 ms | 1012 KiB | 13 Q