3v4l.org

run code in 300+ PHP versions simultaneously
<?php class objectDispatch { public function routeIterator(&$path) { while (!empty($path)) { yield end($path); array_pop($path); } } public function __invoke($context, $root) { $last = ''; $parent = null; $current = $root; $isEndpoint = false; $path = $context->requestPath; foreach($this->routeIterator($path) as $chunk) { if (!is_object($current) && class_exists($current)) { $current = new $current(); } if (is_object($current)) { $parent = $current; } if (in_array($chunk, get_class_methods($parent))) { yield $parent->$chunk($path); $isEndpoint = true; break; } else if (array_key_exists($chunk, get_object_vars($parent))) { $current = $parent->$chunk; } else if (method_exists($parent, 'lookup')) { list($current, $consumed) = $parent->lookup($path); $chunk = implode('/', $consumed); $truncateAt = count($consumed); array_splice($path, $truncateAt, ($truncateAt - count($path))); } else { header('Http/1.0 404 Not Found'); exit(); } $last = $chunk; } //No parts remaining if (!is_object($current) && class_exists($current)) { $current = new $current(); } if (!$isEndpoint) { if (is_object($current)) { yield $current(); } else if (is_object($parent)) { yield $parent(); } } } } class context { public $requestPath; public $trailing; public function __construct($path) { $this->trailing = false; $requestPath = explode('/', $path); if (end($path) == '') { $this->trailing = (count($path) > 2); array_pop($path); } $path = array_reverse($path); if (end($path) == '') { //Eliminate leading slashes array_pop($path); } $this->requestPath = $path; } } class rootController { public $admin = 'adminController'; public function __invoke($args = []) { return "hello from root"; } } class adminController { public $user = 'usersController'; public function __invoke($args = []) { return "hello from admin"; } } class usersController { public function __invoke($args = []) { return "hello from users"; } public function lookup($path) { $current = new userController($path[0]); return [$current, [$path[0]]]; } } class userController { public $id; public function __construct($id) { $this->id = $id; } public function __invoke($args = []) { return "hello from user " . $this->id; } } $context = new context('/admin/user/'); $dispatch = new objectDispatch(); foreach ($dispatch($context, 'rootController') as $dispatchMessage) { echo $dispatchMessage; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/YAmBT
function name:  (null)
number of ops:  17
compiled vars:  !0 = $context, !1 = $dispatch, !2 = $dispatchMessage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  111     0  E >   NEW                                              $3      'context'
          1        SEND_VAL_EX                                              '%2Fadmin%2Fuser%2F'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
  112     4        NEW                                              $6      'objectDispatch'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $6
  113     7        INIT_DYNAMIC_CALL                                        !1
          8        SEND_VAR_EX                                              !0
          9        SEND_VAL_EX                                              'rootController'
         10        DO_FCALL                                      0  $9      
         11      > FE_RESET_R                                       $10     $9, ->15
         12    > > FE_FETCH_R                                               $10, !2, ->15
  114    13    >   ECHO                                                     !2
  113    14      > JMP                                                      ->12
         15    >   FE_FREE                                                  $10
  115    16      > RETURN                                                   1

Class objectDispatch:
Function routeiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 3
Branch analysis from position: 13
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 3
Branch analysis from position: 13
Branch analysis from position: 3
filename:       /in/YAmBT
function name:  routeIterator
number of ops:  14
compiled vars:  !0 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
    4     2      > JMP                                                      ->10
    5     3    >   INIT_FCALL                                               'end'
          4        SEND_REF                                                 !0
          5        DO_ICALL                                         $1      
          6        YIELD                                                    $1
    6     7        INIT_FCALL                                               'array_pop'
          8        SEND_REF                                                 !0
          9        DO_ICALL                                                 
    4    10    >   ISSET_ISEMPTY_CV                                 ~4      !0
         11        BOOL_NOT                                         ~5      ~4
         12      > JMPNZ                                                    ~5, ->3
    8    13    > > GENERATOR_RETURN                                         

End of function routeiterator

Function __invoke:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 86
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 86
Branch analysis from position: 14
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 26
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 44
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
2 jumps found. (Code = 46) Position 1 = 90, Position 2 = 94
Branch analysis from position: 90
2 jumps found. (Code = 43) Position 1 = 95, Position 2 = 99
Branch analysis from position: 95
2 jumps found. (Code = 43) Position 1 = 101, Position 2 = 112
Branch analysis from position: 101
2 jumps found. (Code = 43) Position 1 = 103, Position 2 = 107
Branch analysis from position: 103
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 107
2 jumps found. (Code = 43) Position 1 = 109, Position 2 = 112
Branch analysis from position: 109
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 112
Branch analysis from position: 112
Branch analysis from position: 99
Branch analysis from position: 94
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 52
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 80
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 80
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 29
Branch analysis from position: 26
Branch analysis from position: 21
Branch analysis from position: 86
Branch analysis from position: 86
filename:       /in/YAmBT
function name:  __invoke
number of ops:  113
compiled vars:  !0 = $context, !1 = $root, !2 = $last, !3 = $parent, !4 = $current, !5 = $isEndpoint, !6 = $path, !7 = $chunk, !8 = $consumed, !9 = $truncateAt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   10     3        ASSIGN                                                   !2, ''
   11     4        ASSIGN                                                   !3, null
   12     5        ASSIGN                                                   !4, !1
   13     6        ASSIGN                                                   !5, <false>
   14     7        FETCH_OBJ_R                                      ~14     !0, 'requestPath'
          8        ASSIGN                                                   !6, ~14
   16     9        INIT_METHOD_CALL                                         'routeIterator'
         10        SEND_VAR_EX                                              !6
         11        DO_FCALL                                      0  $16     
         12      > FE_RESET_R                                       $17     $16, ->86
         13    > > FE_FETCH_R                                               $17, !7, ->86
   17    14    >   TYPE_CHECK                                  256  ~18     !4
         15        BOOL_NOT                                         ~19     ~18
         16      > JMPZ_EX                                          ~19     ~19, ->21
         17    >   INIT_FCALL                                               'class_exists'
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                         $20     
         20        BOOL                                             ~19     $20
         21    > > JMPZ                                                     ~19, ->26
   18    22    >   FETCH_CLASS                                   0  $21     !4
         23        NEW                                              $22     $21
         24        DO_FCALL                                      0          
         25        ASSIGN                                                   !4, $22
   20    26    >   TYPE_CHECK                                  256          !4
         27      > JMPZ                                                     ~25, ->29
   21    28    >   ASSIGN                                                   !3, !4
   23    29    >   INIT_FCALL                                               'in_array'
         30        SEND_VAR                                                 !7
         31        INIT_FCALL                                               'get_class_methods'
         32        SEND_VAR                                                 !3
         33        DO_ICALL                                         $27     
         34        SEND_VAR                                                 $27
         35        DO_ICALL                                         $28     
         36      > JMPZ                                                     $28, ->44
   24    37    >   INIT_METHOD_CALL                                         !3, !7
         38        SEND_VAR_EX                                              !6
         39        DO_FCALL                                      0  $29     
         40        YIELD                                                    $29
   25    41        ASSIGN                                                   !5, <true>
   26    42      > JMP                                                      ->86
         43*       JMP                                                      ->84
   27    44    >   INIT_FCALL                                               'get_object_vars'
         45        SEND_VAR                                                 !3
         46        DO_ICALL                                         $32     
         47        ARRAY_KEY_EXISTS                                         !7, $32
         48      > JMPZ                                                     ~33, ->52
   28    49    >   FETCH_OBJ_R                                      ~34     !3, !7
         50        ASSIGN                                                   !4, ~34
         51      > JMP                                                      ->84
   29    52    >   INIT_FCALL                                               'method_exists'
         53        SEND_VAR                                                 !3
         54        SEND_VAL                                                 'lookup'
         55        DO_ICALL                                         $36     
         56      > JMPZ                                                     $36, ->80
   30    57    >   INIT_METHOD_CALL                                         !3, 'lookup'
         58        SEND_VAR_EX                                              !6
         59        DO_FCALL                                      0  $37     
         60        FETCH_LIST_R                                     $38     $37, 0
         61        ASSIGN                                                   !4, $38
         62        FETCH_LIST_R                                     $40     $37, 1
         63        ASSIGN                                                   !8, $40
         64        FREE                                                     $37
   31    65        INIT_FCALL                                               'implode'
         66        SEND_VAL                                                 '%2F'
         67        SEND_VAR                                                 !8
         68        DO_ICALL                                         $42     
         69        ASSIGN                                                   !7, $42
   32    70        COUNT                                            ~44     !8
         71        ASSIGN                                                   !9, ~44
   33    72        INIT_FCALL                                               'array_splice'
         73        SEND_REF                                                 !6
         74        SEND_VAR                                                 !9
         75        COUNT                                            ~46     !6
         76        SUB                                              ~47     !9, ~46
         77        SEND_VAL                                                 ~47
         78        DO_ICALL                                                 
         79      > JMP                                                      ->84
   35    80    >   INIT_FCALL                                               'header'
         81        SEND_VAL                                                 'Http%2F1.0+404+Not+Found'
         82        DO_ICALL                                                 
   36    83      > EXIT                                                     
   38    84    >   ASSIGN                                                   !2, !7
   16    85      > JMP                                                      ->13
         86    >   FE_FREE                                                  $17
   41    87        TYPE_CHECK                                  256  ~51     !4
         88        BOOL_NOT                                         ~52     ~51
         89      > JMPZ_EX                                          ~52     ~52, ->94
         90    >   INIT_FCALL                                               'class_exists'
         91        SEND_VAR                                                 !4
         92        DO_ICALL                                         $53     
         93        BOOL                                             ~52     $53
         94    > > JMPZ                                                     ~52, ->99
   42    95    >   FETCH_CLASS                                   0  $54     !4
         96        NEW                                              $55     $54
         97        DO_FCALL                                      0          
         98        ASSIGN                                                   !4, $55
   44    99    >   BOOL_NOT                                         ~58     !5
        100      > JMPZ                                                     ~58, ->112
   45   101    >   TYPE_CHECK                                  256          !4
        102      > JMPZ                                                     ~59, ->107
   46   103    >   INIT_DYNAMIC_CALL                                        !4
        104        DO_FCALL                                      0  $60     
        105        YIELD                                                    $60
        106      > JMP                                                      ->112
   47   107    >   TYPE_CHECK                                  256          !3
        108      > JMPZ                                                     ~62, ->112
   48   109    >   INIT_DYNAMIC_CALL                                        !3
        110        DO_FCALL                                      0  $63     
        111        YIELD                                                    $63
   51   112    > > GENERATOR_RETURN                                         

End of function __invoke

End of class objectDispatch.

Class context:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 32
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
Branch analysis from position: 20
filename:       /in/YAmBT
function name:  __construct
number of ops:  35
compiled vars:  !0 = $path, !1 = $requestPath
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        ASSIGN_OBJ                                               'trailing'
          2        OP_DATA                                                  <false>
   60     3        INIT_FCALL                                               'explode'
          4        SEND_VAL                                                 '%2F'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !1, $3
   61     8        INIT_FCALL                                               'end'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                         $5      
         11        IS_EQUAL                                                 $5, ''
         12      > JMPZ                                                     ~6, ->20
   62    13    >   COUNT                                            ~8      !0
         14        IS_SMALLER                                       ~9      2, ~8
         15        ASSIGN_OBJ                                               'trailing'
         16        OP_DATA                                                  ~9
   63    17        INIT_FCALL                                               'array_pop'
         18        SEND_REF                                                 !0
         19        DO_ICALL                                                 
   65    20    >   INIT_FCALL                                               'array_reverse'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $11     
         23        ASSIGN                                                   !0, $11
   66    24        INIT_FCALL                                               'end'
         25        SEND_REF                                                 !0
         26        DO_ICALL                                         $13     
         27        IS_EQUAL                                                 $13, ''
         28      > JMPZ                                                     ~14, ->32
   68    29    >   INIT_FCALL                                               'array_pop'
         30        SEND_REF                                                 !0
         31        DO_ICALL                                                 
   70    32    >   ASSIGN_OBJ                                               'requestPath'
         33        OP_DATA                                                  !0
   71    34      > RETURN                                                   null

End of function __construct

End of class context.

Class rootController:
Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV_INIT                                        !0      <array>
   78     1      > RETURN                                                   'hello+from+root'
   79     2*     > RETURN                                                   null

End of function __invoke

End of class rootController.

Class adminController:
Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV_INIT                                        !0      <array>
   85     1      > RETURN                                                   'hello+from+admin'
   86     2*     > RETURN                                                   null

End of function __invoke

End of class adminController.

Class usersController:
Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV_INIT                                        !0      <array>
   91     1      > RETURN                                                   'hello+from+users'
   92     2*     > RETURN                                                   null

End of function __invoke

Function lookup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  lookup
number of ops:  13
compiled vars:  !0 = $path, !1 = $current
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   RECV                                             !0      
   95     1        NEW                                              $2      'userController'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_DIM_FUNC_ARG                               $3      !0, 0
          4        SEND_FUNC_ARG                                            $3
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $2
   96     7        INIT_ARRAY                                       ~6      !1
          8        FETCH_DIM_R                                      ~7      !0, 0
          9        INIT_ARRAY                                       ~8      ~7
         10        ADD_ARRAY_ELEMENT                                ~6      ~8
         11      > RETURN                                                   ~6
   97    12*     > RETURN                                                   null

End of function lookup

End of class usersController.

Class userController:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  __construct
number of ops:  4
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
  103     1        ASSIGN_OBJ                                               'id'
          2        OP_DATA                                                  !0
  104     3      > RETURN                                                   null

End of function __construct

Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YAmBT
function name:  __invoke
number of ops:  5
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  106     0  E >   RECV_INIT                                        !0      <array>
  107     1        FETCH_OBJ_R                                      ~1      'id'
          2        CONCAT                                           ~2      'hello+from+user+', ~1
          3      > RETURN                                                   ~2
  108     4*     > RETURN                                                   null

End of function __invoke

End of class userController.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.6 ms | 1420 KiB | 37 Q