3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Human { public function speak() { return "I can speak"; } public function walk() { return "I can walk"; } public function jump() { return "I can jump"; } } /* calling parent is working but lots of duplication */ /* class Man extends Human { // can speak and walk but overrides some of the human function public function walk() { return parent::walk() . " fast"; } public function speak() { return parent::speak() . " fast"; } } */ class Man { // can speak and walk but overrides some of the human function public function __construct(Human $human) { $this->_human = $human; $this->_override = ["walk", "speak"]; } private function _method($parentResult) { return static::class . ": ". $parentResult . " fast"; } public function __call($name, $arguments) { return in_array($name, $this->_override) ? $this->_method($this->_human->$name()) : $this->_human->$name(); // here we could check if method exists } } // woman is an example for static access to instance methods of parent --> could be also static at parent (just easier to test with the code here) class Woman { private static function _method($parentResult) { return static::class . ": ". $parentResult . " fast"; } public static function __callStatic($name, $arguments) { $_override = ["walk", "speak"]; $_human = new Human(); return in_array($name, $_override) ? self::_method($_human->$name()) : $_human->$name(); // here we could check if method exists } } $human = new Human(); echo $human->speak() . "\r\n"; echo $human->walk(); echo "\r\n"; $man = new Man(new Human()); echo $man->speak() . "\r\n"; echo $man->walk() . "\r\n"; echo $man->jump(); echo "\r\n"; $woman = new Woman(); echo $woman::speak() . "\r\n"; echo $woman::walk() . "\r\n"; echo $woman::jump();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  (null)
number of ops:  47
compiled vars:  !0 = $human, !1 = $man, !2 = $woman
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   NEW                                              $3      'Human'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   66     3        INIT_METHOD_CALL                                         !0, 'speak'
          4        DO_FCALL                                      0  $6      
          5        CONCAT                                           ~7      $6, '%0D%0A'
          6        ECHO                                                     ~7
   67     7        INIT_METHOD_CALL                                         !0, 'walk'
          8        DO_FCALL                                      0  $8      
          9        ECHO                                                     $8
   69    10        ECHO                                                     '%0D%0A'
   70    11        NEW                                              $9      'Man'
         12        NEW                                              $10     'Human'
         13        DO_FCALL                                      0          
         14        SEND_VAR_NO_REF_EX                                       $10
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $9
   71    17        INIT_METHOD_CALL                                         !1, 'speak'
         18        DO_FCALL                                      0  $14     
         19        CONCAT                                           ~15     $14, '%0D%0A'
         20        ECHO                                                     ~15
   72    21        INIT_METHOD_CALL                                         !1, 'walk'
         22        DO_FCALL                                      0  $16     
         23        CONCAT                                           ~17     $16, '%0D%0A'
         24        ECHO                                                     ~17
   73    25        INIT_METHOD_CALL                                         !1, 'jump'
         26        DO_FCALL                                      0  $18     
         27        ECHO                                                     $18
   75    28        ECHO                                                     '%0D%0A'
   76    29        NEW                                              $19     'Woman'
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !2, $19
   77    32        FETCH_CLASS                                   0  $22     !2
         33        INIT_STATIC_METHOD_CALL                                  $22, 'speak'
         34        DO_FCALL                                      0  $23     
         35        CONCAT                                           ~24     $23, '%0D%0A'
         36        ECHO                                                     ~24
   78    37        FETCH_CLASS                                   0  $25     !2
         38        INIT_STATIC_METHOD_CALL                                  $25, 'walk'
         39        DO_FCALL                                      0  $26     
         40        CONCAT                                           ~27     $26, '%0D%0A'
         41        ECHO                                                     ~27
   79    42        FETCH_CLASS                                   0  $28     !2
         43        INIT_STATIC_METHOD_CALL                                  $28, 'jump'
         44        DO_FCALL                                      0  $29     
         45        ECHO                                                     $29
         46      > RETURN                                                   1

Class Human:
Function speak:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  speak
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E > > RETURN                                                   'I+can+speak'
    6     1*     > RETURN                                                   null

End of function speak

Function walk:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  walk
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E > > RETURN                                                   'I+can+walk'
   10     1*     > RETURN                                                   null

End of function walk

Function jump:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  jump
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E > > RETURN                                                   'I+can+jump'
   14     1*     > RETURN                                                   null

End of function jump

End of class Human.

Class Man:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  __construct
number of ops:  6
compiled vars:  !0 = $human
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        ASSIGN_OBJ                                               '_human'
          2        OP_DATA                                                  !0
   36     3        ASSIGN_OBJ                                               '_override'
          4        OP_DATA                                                  <array>
   37     5      > RETURN                                                   null

End of function __construct

Function _method:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  _method
number of ops:  7
compiled vars:  !0 = $parentResult
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        FETCH_CLASS_NAME                                 ~1      
          2        CONCAT                                           ~2      ~1, '%3A+'
          3        CONCAT                                           ~3      ~2, !0
          4        CONCAT                                           ~4      ~3, '+fast'
          5      > RETURN                                                   ~4
   41     6*     > RETURN                                                   null

End of function _method

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 = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  __call
number of ops:  22
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   44     2        INIT_FCALL                                               'in_array'
          3        SEND_VAR                                                 !0
          4        FETCH_OBJ_R                                      ~2      '_override'
          5        SEND_VAL                                                 ~2
          6        DO_ICALL                                         $3      
          7      > JMPZ                                                     $3, ->16
   45     8    >   INIT_METHOD_CALL                                         '_method'
          9        FETCH_OBJ_R                                      ~4      '_human'
         10        INIT_METHOD_CALL                                         ~4, !0
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR                                                 $5
         13        DO_FCALL                                      0  $6      
         14        QM_ASSIGN                                        ~7      $6
         15      > JMP                                                      ->20
   46    16    >   FETCH_OBJ_R                                      ~8      '_human'
         17        INIT_METHOD_CALL                                         ~8, !0
         18        DO_FCALL                                      0  $9      
         19        QM_ASSIGN                                        ~7      $9
         20    > > RETURN                                                   ~7
   47    21*     > RETURN                                                   null

End of function __call

End of class Man.

Class Woman:
Function _method:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  _method
number of ops:  7
compiled vars:  !0 = $parentResult
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        FETCH_CLASS_NAME                                 ~1      
          2        CONCAT                                           ~2      ~1, '%3A+'
          3        CONCAT                                           ~3      ~2, !0
          4        CONCAT                                           ~4      ~3, '+fast'
          5      > RETURN                                                   ~4
   54     6*     > RETURN                                                   null

End of function _method

Function __callstatic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BYYDl
function name:  __callStatic
number of ops:  23
compiled vars:  !0 = $name, !1 = $arguments, !2 = $_override, !3 = $_human
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   57     2        ASSIGN                                                   !2, <array>
   58     3        NEW                                              $5      'Human'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !3, $5
   59     6        INIT_FCALL                                               'in_array'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $8      
         10      > JMPZ                                                     $8, ->18
   60    11    >   INIT_STATIC_METHOD_CALL                                  '_method'
         12        INIT_METHOD_CALL                                         !3, !0
         13        DO_FCALL                                      0  $9      
         14        SEND_VAR                                                 $9
         15        DO_FCALL                                      0  $10     
         16        QM_ASSIGN                                        ~11     $10
         17      > JMP                                                      ->21
   61    18    >   INIT_METHOD_CALL                                         !3, !0
         19        DO_FCALL                                      0  $12     
         20        QM_ASSIGN                                        ~11     $12
         21    > > RETURN                                                   ~11
   62    22*     > RETURN                                                   null

End of function __callstatic

End of class Woman.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.11 ms | 1413 KiB | 15 Q