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) { $trailing = false; $path = explode('/', $context->requestPath); if (end($path) == '') { $trailing = (count($path) > 2); array_pop($path); } $path = array_reverse($path); if (end($path) == '') { //Eliminate leading slashes array_pop($path); } $last = ''; $parent = null; $current = $root; $isEndpoint = false; 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 function __construct($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/Iv7Od
function name:  (null)
number of ops:  17
compiled vars:  !0 = $context, !1 = $dispatch, !2 = $dispatchMessage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   NEW                                              $3      'context'
          1        SEND_VAL_EX                                              '%2Fadmin%2Fuser%2F'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
  109     4        NEW                                              $6      'objectDispatch'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $6
  110     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
  111    13    >   ECHO                                                     !2
  110    14      > JMP                                                      ->12
         15    >   FE_FREE                                                  $10
  112    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/Iv7Od
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 = 43) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 33
Branch analysis from position: 30
2 jumps found. (Code = 77) Position 1 = 41, Position 2 = 114
Branch analysis from position: 41
2 jumps found. (Code = 78) Position 1 = 42, Position 2 = 114
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 45, Position 2 = 49
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 54
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 57
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 72
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 114
Branch analysis from position: 114
2 jumps found. (Code = 46) Position 1 = 118, Position 2 = 122
Branch analysis from position: 118
2 jumps found. (Code = 43) Position 1 = 123, Position 2 = 127
Branch analysis from position: 123
2 jumps found. (Code = 43) Position 1 = 129, Position 2 = 140
Branch analysis from position: 129
2 jumps found. (Code = 43) Position 1 = 131, Position 2 = 135
Branch analysis from position: 131
1 jumps found. (Code = 42) Position 1 = 140
Branch analysis from position: 140
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 135
2 jumps found. (Code = 43) Position 1 = 137, Position 2 = 140
Branch analysis from position: 137
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 140
Branch analysis from position: 140
Branch analysis from position: 127
Branch analysis from position: 122
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 80
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 80
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 108
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
Branch analysis from position: 108
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 57
Branch analysis from position: 54
Branch analysis from position: 49
Branch analysis from position: 114
Branch analysis from position: 114
Branch analysis from position: 33
Branch analysis from position: 21
filename:       /in/Iv7Od
function name:  __invoke
number of ops:  141
compiled vars:  !0 = $context, !1 = $root, !2 = $trailing, !3 = $path, !4 = $last, !5 = $parent, !6 = $current, !7 = $isEndpoint, !8 = $chunk, !9 = $consumed, !10 = $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, <false>
   11     4        INIT_FCALL                                               'explode'
          5        SEND_VAL                                                 '%2F'
          6        FETCH_OBJ_R                                      ~12     !0, 'requestPath'
          7        SEND_VAL                                                 ~12
          8        DO_ICALL                                         $13     
          9        ASSIGN                                                   !3, $13
   12    10        INIT_FCALL                                               'end'
         11        SEND_REF                                                 !3
         12        DO_ICALL                                         $15     
         13        IS_EQUAL                                                 $15, ''
         14      > JMPZ                                                     ~16, ->21
   13    15    >   COUNT                                            ~17     !3
         16        IS_SMALLER                                       ~18     2, ~17
         17        ASSIGN                                                   !2, ~18
   14    18        INIT_FCALL                                               'array_pop'
         19        SEND_REF                                                 !3
         20        DO_ICALL                                                 
   16    21    >   INIT_FCALL                                               'array_reverse'
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $21     
         24        ASSIGN                                                   !3, $21
   17    25        INIT_FCALL                                               'end'
         26        SEND_REF                                                 !3
         27        DO_ICALL                                         $23     
         28        IS_EQUAL                                                 $23, ''
         29      > JMPZ                                                     ~24, ->33
   19    30    >   INIT_FCALL                                               'array_pop'
         31        SEND_REF                                                 !3
         32        DO_ICALL                                                 
   21    33    >   ASSIGN                                                   !4, ''
   22    34        ASSIGN                                                   !5, null
   23    35        ASSIGN                                                   !6, !1
   24    36        ASSIGN                                                   !7, <false>
   25    37        INIT_METHOD_CALL                                         'routeIterator'
         38        SEND_VAR_EX                                              !3
         39        DO_FCALL                                      0  $30     
         40      > FE_RESET_R                                       $31     $30, ->114
         41    > > FE_FETCH_R                                               $31, !8, ->114
   26    42    >   TYPE_CHECK                                  256  ~32     !6
         43        BOOL_NOT                                         ~33     ~32
         44      > JMPZ_EX                                          ~33     ~33, ->49
         45    >   INIT_FCALL                                               'class_exists'
         46        SEND_VAR                                                 !6
         47        DO_ICALL                                         $34     
         48        BOOL                                             ~33     $34
         49    > > JMPZ                                                     ~33, ->54
   27    50    >   FETCH_CLASS                                   0  $35     !6
         51        NEW                                              $36     $35
         52        DO_FCALL                                      0          
         53        ASSIGN                                                   !6, $36
   29    54    >   TYPE_CHECK                                  256          !6
         55      > JMPZ                                                     ~39, ->57
   30    56    >   ASSIGN                                                   !5, !6
   32    57    >   INIT_FCALL                                               'in_array'
         58        SEND_VAR                                                 !8
         59        INIT_FCALL                                               'get_class_methods'
         60        SEND_VAR                                                 !5
         61        DO_ICALL                                         $41     
         62        SEND_VAR                                                 $41
         63        DO_ICALL                                         $42     
         64      > JMPZ                                                     $42, ->72
   33    65    >   INIT_METHOD_CALL                                         !5, !8
         66        SEND_VAR_EX                                              !3
         67        DO_FCALL                                      0  $43     
         68        YIELD                                                    $43
   34    69        ASSIGN                                                   !7, <true>
   35    70      > JMP                                                      ->114
         71*       JMP                                                      ->112
   36    72    >   INIT_FCALL                                               'get_object_vars'
         73        SEND_VAR                                                 !5
         74        DO_ICALL                                         $46     
         75        ARRAY_KEY_EXISTS                                         !8, $46
         76      > JMPZ                                                     ~47, ->80
   37    77    >   FETCH_OBJ_R                                      ~48     !5, !8
         78        ASSIGN                                                   !6, ~48
         79      > JMP                                                      ->112
   38    80    >   INIT_FCALL                                               'method_exists'
         81        SEND_VAR                                                 !5
         82        SEND_VAL                                                 'lookup'
         83        DO_ICALL                                         $50     
         84      > JMPZ                                                     $50, ->108
   39    85    >   INIT_METHOD_CALL                                         !5, 'lookup'
         86        SEND_VAR_EX                                              !3
         87        DO_FCALL                                      0  $51     
         88        FETCH_LIST_R                                     $52     $51, 0
         89        ASSIGN                                                   !6, $52
         90        FETCH_LIST_R                                     $54     $51, 1
         91        ASSIGN                                                   !9, $54
         92        FREE                                                     $51
   40    93        INIT_FCALL                                               'implode'
         94        SEND_VAL                                                 '%2F'
         95        SEND_VAR                                                 !9
         96        DO_ICALL                                         $56     
         97        ASSIGN                                                   !8, $56
   41    98        COUNT                                            ~58     !9
         99        ASSIGN                                                   !10, ~58
   42   100        INIT_FCALL                                               'array_splice'
        101        SEND_REF                                                 !3
        102        SEND_VAR                                                 !10
        103        COUNT                                            ~60     !3
        104        SUB                                              ~61     !10, ~60
        105        SEND_VAL                                                 ~61
        106        DO_ICALL                                                 
        107      > JMP                                                      ->112
   44   108    >   INIT_FCALL                                               'header'
        109        SEND_VAL                                                 'Http%2F1.0+404+Not+Found'
        110        DO_ICALL                                                 
   45   111      > EXIT                                                     
   47   112    >   ASSIGN                                                   !4, !8
   25   113      > JMP                                                      ->41
        114    >   FE_FREE                                                  $31
   50   115        TYPE_CHECK                                  256  ~65     !6
        116        BOOL_NOT                                         ~66     ~65
        117      > JMPZ_EX                                          ~66     ~66, ->122
        118    >   INIT_FCALL                                               'class_exists'
        119        SEND_VAR                                                 !6
        120        DO_ICALL                                         $67     
        121        BOOL                                             ~66     $67
        122    > > JMPZ                                                     ~66, ->127
   51   123    >   FETCH_CLASS                                   0  $68     !6
        124        NEW                                              $69     $68
        125        DO_FCALL                                      0          
        126        ASSIGN                                                   !6, $69
   53   127    >   BOOL_NOT                                         ~72     !7
        128      > JMPZ                                                     ~72, ->140
   54   129    >   TYPE_CHECK                                  256          !6
        130      > JMPZ                                                     ~73, ->135
   55   131    >   INIT_DYNAMIC_CALL                                        !6
        132        DO_FCALL                                      0  $74     
        133        YIELD                                                    $74
        134      > JMP                                                      ->140
   56   135    >   TYPE_CHECK                                  256          !5
        136      > JMPZ                                                     ~76, ->140
   57   137    >   INIT_DYNAMIC_CALL                                        !5
        138        DO_FCALL                                      0  $77     
        139        YIELD                                                    $77
   60   140    > > GENERATOR_RETURN                                         

End of function __invoke

End of class objectDispatch.

Class context:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Iv7Od
function name:  __construct
number of ops:  4
compiled vars:  !0 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
   67     1        ASSIGN_OBJ                                               'requestPath'
          2        OP_DATA                                                  !0
   68     3      > 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/Iv7Od
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV_INIT                                        !0      <array>
   75     1      > RETURN                                                   'hello+from+root'
   76     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/Iv7Od
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV_INIT                                        !0      <array>
   82     1      > RETURN                                                   'hello+from+admin'
   83     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/Iv7Od
function name:  __invoke
number of ops:  3
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   87     0  E >   RECV_INIT                                        !0      <array>
   88     1      > RETURN                                                   'hello+from+users'
   89     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/Iv7Od
function name:  lookup
number of ops:  13
compiled vars:  !0 = $path, !1 = $current
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   RECV                                             !0      
   92     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
   93     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
   94    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/Iv7Od
function name:  __construct
number of ops:  4
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   RECV                                             !0      
  100     1        ASSIGN_OBJ                                               'id'
          2        OP_DATA                                                  !0
  101     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/Iv7Od
function name:  __invoke
number of ops:  5
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  103     0  E >   RECV_INIT                                        !0      <array>
  104     1        FETCH_OBJ_R                                      ~1      'id'
          2        CONCAT                                           ~2      'hello+from+user+', ~1
          3      > RETURN                                                   ~2
  105     4*     > RETURN                                                   null

End of function __invoke

End of class userController.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.39 ms | 1425 KiB | 37 Q