3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Delegator { private $delegates = []; protected function delegate($object, array $methods) { // check against duplicate methods $this->delegates[] = ['object' => $object, 'methods' => $methods]; } function __call($method, array $args) { foreach ($this->delegates as $delegate) { if (in_array($method, $delegate['methods'])) { return call_user_func_array([$delegate['object'], $method], $args); } } throw new Exception('Method not found!'); } } class AuthService { function authenticate($username, $password) { echo 'Doing the auth for ' . $username . '!'; } } class UserModel { use Delegator; function __construct(AuthService $auth) { $this->delegate($auth, ['authenticate']); } } $auth = new AuthService(); $model = new UserModel($auth); $model->authenticate('test', 'password'); $model->notFound();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXMo
function name:  (null)
number of ops:  15
compiled vars:  !0 = $auth, !1 = $model
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   DECLARE_CLASS                                            'usermodel'
   42     1        NEW                                              $2      'AuthService'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   43     4        NEW                                              $5      'UserModel'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $5
   45     8        INIT_METHOD_CALL                                         !1, 'authenticate'
          9        SEND_VAL_EX                                              'test'
         10        SEND_VAL_EX                                              'password'
         11        DO_FCALL                                      0          
   46    12        INIT_METHOD_CALL                                         !1, 'notFound'
         13        DO_FCALL                                      0          
         14      > RETURN                                                   1

Class Delegator:
Function delegate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXMo
function name:  delegate
number of ops:  8
compiled vars:  !0 = $object, !1 = $methods
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        INIT_ARRAY                                       ~4      !0, 'object'
          3        ADD_ARRAY_ELEMENT                                ~4      !1, 'methods'
          4        FETCH_OBJ_W                                      $2      'delegates'
          5        ASSIGN_DIM                                               $2
          6        OP_DATA                                                  ~4
   10     7      > RETURN                                                   null

End of function delegate

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 21
filename:       /in/GBXMo
function name:  __call
number of ops:  27
compiled vars:  !0 = $method, !1 = $args, !2 = $delegate
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        FETCH_OBJ_R                                      ~3      'delegates'
          3      > FE_RESET_R                                       $4      ~3, ->21
          4    > > FE_FETCH_R                                               $4, !2, ->21
   14     5    >   INIT_FCALL                                               'in_array'
          6        SEND_VAR                                                 !0
          7        FETCH_DIM_R                                      ~5      !2, 'methods'
          8        SEND_VAL                                                 ~5
          9        DO_ICALL                                         $6      
         10      > JMPZ                                                     $6, ->20
   15    11    >   FETCH_DIM_R                                      ~7      !2, 'object'
         12        INIT_ARRAY                                       ~8      ~7
         13        ADD_ARRAY_ELEMENT                                ~8      !0
         14        INIT_USER_CALL                                0          'call_user_func_array', ~8
         15        SEND_ARRAY                                               !1
         16        CHECK_UNDEF_ARGS                                         
         17        DO_FCALL                                      0  $9      
         18        FE_FREE                                                  $4
         19      > RETURN                                                   $9
   13    20    > > JMP                                                      ->4
         21    >   FE_FREE                                                  $4
   19    22        NEW                                              $10     'Exception'
         23        SEND_VAL_EX                                              'Method+not+found%21'
         24        DO_FCALL                                      0          
         25      > THROW                                         0          $10
   20    26*     > RETURN                                                   null

End of function __call

End of class Delegator.

Class AuthService:
Function authenticate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXMo
function name:  authenticate
number of ops:  6
compiled vars:  !0 = $username, !1 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   27     2        CONCAT                                           ~2      'Doing+the+auth+for+', !0
          3        CONCAT                                           ~3      ~2, '%21'
          4        ECHO                                                     ~3
   28     5      > RETURN                                                   null

End of function authenticate

End of class AuthService.

Class UserModel:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXMo
function name:  __construct
number of ops:  6
compiled vars:  !0 = $auth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        INIT_METHOD_CALL                                         'delegate'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0          
   38     5      > RETURN                                                   null

End of function __construct

End of class UserModel.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.6 ms | 1400 KiB | 15 Q