3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace Dshafik; class Struct { // Because gettype() and ReflectionType->__toString() are inconsistent /** * @var array Type mappings */ private $__typeMap = [ 'int' => 'integer', 'bool' => 'boolean', 'float' => 'double', ]; /** * @var array Reflection data */ private $__reflection = []; /** * @var array The struct values */ protected $__values = []; public function __construct() { $this->introspect(); $this->hoist(func_get_args()); } public function __get($what) { return $this->__values[$what]; } public function __set($what, $value) { if (($type = gettype($value)) === $this->__reflection[$what]) { return $this->__values[$what] = $value; } else { throw new \TypeError("Key '$what' must be of type {$this->__reflection[$what]}, $type given."); } } final protected function introspect() { if ($this->__reflection) { return; } $class = new \ReflectionObject($this); $constructor = $class->getConstructor(); foreach ($constructor->getParameters() as $arg) { $type = (string) $arg->getType(); $this->__reflection[$arg->name] = $this->__typeMap[$type] ?? $type; } } final protected function hoist($args) { $this->introspect(); $i = 0; foreach ($this->__reflection as $key => $type) { $this->__set($key, $args[$i]); $i++; } } } // With strict types, will throw a TypeError, or coerce otherwise on mismatch $struct = new class(1, "Hello") extends Struct { public function __construct(int $foo, string $bar) { $this->hoist(func_get_args()); } }; var_dump($struct); // Works $struct->foo = 2; var_dump($struct); // Will throw a TypeError $struct->foo = 'World';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dC8Lu
function name:  (null)
number of ops:  17
compiled vars:  !0 = $struct
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   DECLARE_ANON_CLASS                               <unknown> 'dshafik%5Cstruct'
          1        NEW                                              $2      $1
          2        SEND_VAL_EX                                              1
          3        SEND_VAL_EX                                              'Hello'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   79     6        INIT_NS_FCALL_BY_NAME                                    'Dshafik%5Cvar_dump'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
   82     9        ASSIGN_OBJ                                               !0, 'foo'
         10        OP_DATA                                                  2
   83    11        INIT_NS_FCALL_BY_NAME                                    'Dshafik%5Cvar_dump'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
   86    14        ASSIGN_OBJ                                               !0, 'foo'
         15        OP_DATA                                                  'World'
         16      > RETURN                                                   1

Class Dshafik\Struct:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dC8Lu
function name:  __construct
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   INIT_METHOD_CALL                                         'introspect'
          1        DO_FCALL                                      0          
   29     2        INIT_METHOD_CALL                                         'hoist'
          3        INIT_NS_FCALL_BY_NAME                                    'Dshafik%5Cfunc_get_args'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        DO_FCALL                                      0          
   30     7      > RETURN                                                   null

End of function __construct

Function __get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dC8Lu
function name:  __get
number of ops:  5
compiled vars:  !0 = $what
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   34     1        FETCH_OBJ_R                                      ~1      '__values'
          2        FETCH_DIM_R                                      ~2      ~1, !0
          3      > RETURN                                                   ~2
   35     4*     > RETURN                                                   null

End of function __get

Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/dC8Lu
function name:  __set
number of ops:  29
compiled vars:  !0 = $what, !1 = $value, !2 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   39     2        INIT_NS_FCALL_BY_NAME                                    'Dshafik%5Cgettype'
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                           ~4      !2, $3
          6        FETCH_OBJ_R                                      ~5      '__reflection'
          7        FETCH_DIM_R                                      ~6      ~5, !0
          8        IS_IDENTICAL                                             ~4, ~6
          9      > JMPZ                                                     ~7, ->15
   40    10    >   FETCH_OBJ_W                                      $8      '__values'
         11        ASSIGN_DIM                                       ~9      $8, !0
         12        OP_DATA                                                  !1
         13      > RETURN                                                   ~9
         14*       JMP                                                      ->28
   42    15    >   NEW                                              $10     'TypeError'
         16        ROPE_INIT                                     7  ~14     'Key+%27'
         17        ROPE_ADD                                      1  ~14     ~14, !0
         18        ROPE_ADD                                      2  ~14     ~14, '%27+must+be+of+type+'
         19        FETCH_OBJ_R                                      ~11     '__reflection'
         20        FETCH_DIM_R                                      ~12     ~11, !0
         21        ROPE_ADD                                      3  ~14     ~14, ~12
         22        ROPE_ADD                                      4  ~14     ~14, '%2C+'
         23        ROPE_ADD                                      5  ~14     ~14, !2
         24        ROPE_END                                      6  ~13     ~14, '+given.'
         25        SEND_VAL_EX                                              ~13
         26        DO_FCALL                                      0          
         27      > THROW                                         0          $10
   44    28*     > RETURN                                                   null

End of function __set

Function introspect:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 3
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 28
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 28
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/dC8Lu
function name:  introspect
number of ops:  30
compiled vars:  !0 = $class, !1 = $constructor, !2 = $arg, !3 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   FETCH_OBJ_R                                      ~4      '__reflection'
          1      > JMPZ                                                     ~4, ->3
   49     2    > > RETURN                                                   null
   52     3    >   NEW                                              $5      'ReflectionObject'
          4        FETCH_THIS                                       $6      
          5        SEND_VAR_EX                                              $6
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $5
   53     8        INIT_METHOD_CALL                                         !0, 'getConstructor'
          9        DO_FCALL                                      0  $9      
         10        ASSIGN                                                   !1, $9
   54    11        INIT_METHOD_CALL                                         !1, 'getParameters'
         12        DO_FCALL                                      0  $11     
         13      > FE_RESET_R                                       $12     $11, ->28
         14    > > FE_FETCH_R                                               $12, !2, ->28
   55    15    >   INIT_METHOD_CALL                                         !2, 'getType'
         16        DO_FCALL                                      0  $13     
         17        CAST                                          6  ~14     $13
         18        ASSIGN                                                   !3, ~14
   56    19        FETCH_OBJ_R                                      ~17     !2, 'name'
         20        FETCH_OBJ_IS                                     ~19     '__typeMap'
         21        FETCH_DIM_IS                                     ~20     ~19, !3
         22        COALESCE                                         ~21     ~20
         23        QM_ASSIGN                                        ~21     !3
         24        FETCH_OBJ_W                                      $16     '__reflection'
         25        ASSIGN_DIM                                               $16, ~17
         26        OP_DATA                                                  ~21
   54    27      > JMP                                                      ->14
         28    >   FE_FREE                                                  $12
   58    29      > RETURN                                                   null

End of function introspect

Function hoist:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/dC8Lu
function name:  hoist
number of ops:  18
compiled vars:  !0 = $args, !1 = $i, !2 = $type, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   62     1        INIT_METHOD_CALL                                         'introspect'
          2        DO_FCALL                                      0          
   64     3        ASSIGN                                                   !1, 0
   65     4        FETCH_OBJ_R                                      ~6      '__reflection'
          5      > FE_RESET_R                                       $7      ~6, ->16
          6    > > FE_FETCH_R                                       ~8      $7, !2, ->16
          7    >   ASSIGN                                                   !3, ~8
   66     8        INIT_METHOD_CALL                                         '__set'
          9        SEND_VAR_EX                                              !3
         10        CHECK_FUNC_ARG                                           
         11        FETCH_DIM_FUNC_ARG                               $10     !0, !1
         12        SEND_FUNC_ARG                                            $10
         13        DO_FCALL                                      0          
   67    14        PRE_INC                                                  !1
   65    15      > JMP                                                      ->6
         16    >   FE_FREE                                                  $7
   69    17      > RETURN                                                   null

End of function hoist

End of class Dshafik\Struct.

Class Dshafik\Struct@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dC8Lu
function name:  __construct
number of ops:  8
compiled vars:  !0 = $foo, !1 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   75     2        INIT_METHOD_CALL                                         'hoist'
          3        INIT_NS_FCALL_BY_NAME                                    'Dshafik%5Cfunc_get_args'
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR_NO_REF_EX                                       $2
          6        DO_FCALL                                      0          
   76     7      > RETURN                                                   null

End of function __construct

End of class Dshafik\Struct@anonymous.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.67 ms | 1413 KiB | 19 Q