3v4l.org

run code in 300+ PHP versions simultaneously
<?php $properties = [ 'name' => ['f'], 'name2' => ['relation->g'], # one possibility 'name3' => ['relation', 'g'], # another possibility, (could just be an explode('->')) 'name4' => ['relation', ['format', 'd-m-Y']], # chaining a 2nd method call ]; class BasicModel { public $f = 123; public function relation() { return new RelatedModel; } public function __get($name) { if (method_exists($this, $name)) { return $this->$name(); } } public function __call($name, $arguments) { if (method_exists($this, $name)) { return call_user_func_array ([$this, $name], $arguments); } } } class RelatedModel extends BasicModel # extends to get __get, __call functionality { public $g = 567; public function format($format) { return $format; # tbi } } # here is implementation $model = new BasicModel; foreach ($properties as $name => $_) { // match $method = $_[0]; // call $data[$name] = consume($model, $method); # here, is there an easy way to support all above? or something very close? } function consume($model, $methods) { var_dump(count($methods)); if (count($methods) == 1) { if (strpos($methods[0], '->') !== false) { // there are '->', so explode and repeat return consume($model, explode('->', $methods)); # second case } return $model->$methods; # first case } else { var_dump('not one', $methods); $part = $model; foreach ($methods as $chainCall) { var_dump($part); $part = $part->$chainCall; } return $part; } } var_dump($data);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/oEL6a
function name:  (null)
number of ops:  21
compiled vars:  !0 = $properties, !1 = $model, !2 = $_, !3 = $name, !4 = $method, !5 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   48     1        NEW                                              $7      'BasicModel'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $7
   50     4      > FE_RESET_R                                       $10     !0, ->16
          5    > > FE_FETCH_R                                       ~11     $10, !2, ->16
          6    >   ASSIGN                                                   !3, ~11
   53     7        FETCH_DIM_R                                      ~13     !2, 0
          8        ASSIGN                                                   !4, ~13
   56     9        INIT_FCALL_BY_NAME                                       'consume'
         10        SEND_VAR_EX                                              !1
         11        SEND_VAR_EX                                              !4
         12        DO_FCALL                                      0  $16     
         13        ASSIGN_DIM                                               !5, !3
         14        OP_DATA                                                  $16
   50    15      > JMP                                                      ->5
         16    >   FE_FREE                                                  $10
   86    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !5
         19        DO_ICALL                                                 
         20      > RETURN                                                   1

Function consume:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 28
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 41
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 41
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/oEL6a
function name:  consume
number of ops:  44
compiled vars:  !0 = $model, !1 = $methods, !2 = $part, !3 = $chainCall
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   61     2        INIT_FCALL                                               'var_dump'
          3        COUNT                                            ~4      !1
          4        SEND_VAL                                                 ~4
          5        DO_ICALL                                                 
   62     6        COUNT                                            ~6      !1
          7        IS_EQUAL                                                 ~6, 1
          8      > JMPZ                                                     ~7, ->28
   64     9    >   INIT_FCALL                                               'strpos'
         10        FETCH_DIM_R                                      ~8      !1, 0
         11        SEND_VAL                                                 ~8
         12        SEND_VAL                                                 '-%3E'
         13        DO_ICALL                                         $9      
         14        TYPE_CHECK                                  1018          $9
         15      > JMPZ                                                     ~10, ->25
   67    16    >   INIT_FCALL_BY_NAME                                       'consume'
         17        SEND_VAR_EX                                              !0
         18        INIT_FCALL                                               'explode'
         19        SEND_VAL                                                 '-%3E'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $11     
         22        SEND_VAR_NO_REF_EX                                       $11
         23        DO_FCALL                                      0  $12     
         24      > RETURN                                                   $12
   70    25    >   FETCH_OBJ_R                                      ~13     !0, !1
         26      > RETURN                                                   ~13
         27*       JMP                                                      ->43
   74    28    >   INIT_FCALL                                               'var_dump'
         29        SEND_VAL                                                 'not+one'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                                 
   75    32        ASSIGN                                                   !2, !0
   76    33      > FE_RESET_R                                       $16     !1, ->41
         34    > > FE_FETCH_R                                               $16, !3, ->41
   78    35    >   INIT_FCALL                                               'var_dump'
         36        SEND_VAR                                                 !2
         37        DO_ICALL                                                 
   79    38        FETCH_OBJ_R                                      ~18     !2, !3
         39        ASSIGN                                                   !2, ~18
   76    40      > JMP                                                      ->34
         41    >   FE_FREE                                                  $16
   82    42      > RETURN                                                   !2
   84    43*     > RETURN                                                   null

End of function consume

Class BasicModel:
Function relation:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  relation
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   NEW                                              $0      'RelatedModel'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   $0
   17     3*     > RETURN                                                   null

End of function relation

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  __get
number of ops:  11
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        INIT_FCALL                                               'method_exists'
          2        FETCH_THIS                                       ~1      
          3        SEND_VAL                                                 ~1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6      > JMPZ                                                     $2, ->10
   23     7    >   INIT_METHOD_CALL                                         !0
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   25    10    > > RETURN                                                   null

End of function __get

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  __call
number of ops:  17
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   29     2        INIT_FCALL                                               'method_exists'
          3        FETCH_THIS                                       ~2      
          4        SEND_VAL                                                 ~2
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7      > JMPZ                                                     $3, ->16
   31     8    >   FETCH_THIS                                       ~4      
          9        INIT_ARRAY                                       ~5      ~4
         10        ADD_ARRAY_ELEMENT                                ~5      !0
         11        INIT_USER_CALL                                0          'call_user_func_array', ~5
         12        SEND_ARRAY                                               !1
         13        CHECK_UNDEF_ARGS                                         
         14        DO_FCALL                                      0  $6      
         15      > RETURN                                                   $6
   33    16    > > RETURN                                                   null

End of function __call

End of class BasicModel.

Class RelatedModel:
Function format:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  format
number of ops:  3
compiled vars:  !0 = $format
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   42     1      > RETURN                                                   !0
   43     2*     > RETURN                                                   null

End of function format

Function relation:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  relation
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   NEW                                              $0      'RelatedModel'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   $0
   17     3*     > RETURN                                                   null

End of function relation

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  __get
number of ops:  11
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        INIT_FCALL                                               'method_exists'
          2        FETCH_THIS                                       ~1      
          3        SEND_VAL                                                 ~1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6      > JMPZ                                                     $2, ->10
   23     7    >   INIT_METHOD_CALL                                         !0
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   25    10    > > RETURN                                                   null

End of function __get

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oEL6a
function name:  __call
number of ops:  17
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   29     2        INIT_FCALL                                               'method_exists'
          3        FETCH_THIS                                       ~2      
          4        SEND_VAL                                                 ~2
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7      > JMPZ                                                     $3, ->16
   31     8    >   FETCH_THIS                                       ~4      
          9        INIT_ARRAY                                       ~5      ~4
         10        ADD_ARRAY_ELEMENT                                ~5      !0
         11        INIT_USER_CALL                                0          'call_user_func_array', ~5
         12        SEND_ARRAY                                               !1
         13        CHECK_UNDEF_ARGS                                         
         14        DO_FCALL                                      0  $6      
         15      > RETURN                                                   $6
   33    16    > > RETURN                                                   null

End of function __call

End of class RelatedModel.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.77 ms | 1408 KiB | 21 Q