3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Request { private $method; private $path; function __construct($method, $path) { $this->method = $method; $this->path = $path; } function getMethod() { return $this->method; } function getPath() { return $this->path; } } class Router { private $routes = [ 'get' => [], 'post' => [] ]; function get($pattern, callable $handler) { $this->routes['get'][$pattern] = $handler; return $this; } function post($pattern, callable $handler) { $this->routes['post'][$pattern] = $handler; return $this; } function match(Request $request) { $method = strtolower($request->getMethod()); if (!isset($this->routes[$method])) { return false; } $path = $request->getPath(); foreach ($this->routes[$method] as $pattern => $handler) { if ($pattern === $path) { return $handler; } } return false; } } class Dispatcher { private $router; function __construct(Router $router) { $this->router = $router; } function handle(Request $request) { $handler = $this->router->match($request); if (!$handler) { echo "Could not find your resource!\n"; return; } $handler(); } } $router = new Router(); $router->get('foo', function() { echo "GET foo\n"; }); $router->post('bar', function() { echo "POST bar\n"; }); $dispatcher = new Dispatcher($router); $foo = new Request('GET', 'foo'); $bar = new Request('POST', 'bar'); $qux = new Request('GET', 'qux'); $dispatcher->handle($foo); $dispatcher->handle($bar); $dispatcher->handle($qux);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  (null)
number of ops:  42
compiled vars:  !0 = $router, !1 = $dispatcher, !2 = $foo, !3 = $bar, !4 = $qux
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   NEW                                              $5      'Router'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $5
   80     3        INIT_METHOD_CALL                                         !0, 'get'
          4        SEND_VAL_EX                                              'foo'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FTgBhJ%3A80%240'
          6        SEND_VAL_EX                                              ~8
          7        DO_FCALL                                      0          
   81     8        INIT_METHOD_CALL                                         !0, 'post'
          9        SEND_VAL_EX                                              'bar'
         10        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FTgBhJ%3A81%241'
         11        SEND_VAL_EX                                              ~10
         12        DO_FCALL                                      0          
   83    13        NEW                                              $12     'Dispatcher'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $12
   85    17        NEW                                              $15     'Request'
         18        SEND_VAL_EX                                              'GET'
         19        SEND_VAL_EX                                              'foo'
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !2, $15
   86    22        NEW                                              $18     'Request'
         23        SEND_VAL_EX                                              'POST'
         24        SEND_VAL_EX                                              'bar'
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !3, $18
   87    27        NEW                                              $21     'Request'
         28        SEND_VAL_EX                                              'GET'
         29        SEND_VAL_EX                                              'qux'
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !4, $21
   89    32        INIT_METHOD_CALL                                         !1, 'handle'
         33        SEND_VAR_EX                                              !2
         34        DO_FCALL                                      0          
   90    35        INIT_METHOD_CALL                                         !1, 'handle'
         36        SEND_VAR_EX                                              !3
         37        DO_FCALL                                      0          
   91    38        INIT_METHOD_CALL                                         !1, 'handle'
         39        SEND_VAR_EX                                              !4
         40        DO_FCALL                                      0          
         41      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FTgBhJ%3A80%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   ECHO                                                     'GET+foo%0A'
          1      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FTgBhJ%3A80%240

Function %00%7Bclosure%7D%2Fin%2FTgBhJ%3A81%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   ECHO                                                     'POST+bar%0A'
          1      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FTgBhJ%3A81%241

Class Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  __construct
number of ops:  7
compiled vars:  !0 = $method, !1 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        ASSIGN_OBJ                                               'method'
          3        OP_DATA                                                  !0
   10     4        ASSIGN_OBJ                                               'path'
          5        OP_DATA                                                  !1
   11     6      > RETURN                                                   null

End of function __construct

Function getmethod:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  getMethod
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   FETCH_OBJ_R                                      ~0      'method'
          1      > RETURN                                                   ~0
   15     2*     > RETURN                                                   null

End of function getmethod

Function getpath:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  getPath
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   FETCH_OBJ_R                                      ~0      'path'
          1      > RETURN                                                   ~0
   19     2*     > RETURN                                                   null

End of function getpath

End of class Request.

Class Router:
Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  get
number of ops:  9
compiled vars:  !0 = $pattern, !1 = $handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        FETCH_OBJ_W                                      $2      'routes'
          3        FETCH_DIM_W                                      $3      $2, 'get'
          4        ASSIGN_DIM                                               $3, !0
          5        OP_DATA                                                  !1
   33     6        FETCH_THIS                                       ~5      
          7      > RETURN                                                   ~5
   34     8*     > RETURN                                                   null

End of function get

Function post:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  post
number of ops:  9
compiled vars:  !0 = $pattern, !1 = $handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2        FETCH_OBJ_W                                      $2      'routes'
          3        FETCH_DIM_W                                      $3      $2, 'post'
          4        ASSIGN_DIM                                               $3, !0
          5        OP_DATA                                                  !1
   38     6        FETCH_THIS                                       ~5      
          7      > RETURN                                                   ~5
   39     8*     > RETURN                                                   null

End of function post

Function match:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 25
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/TgBhJ
function name:  match
number of ops:  28
compiled vars:  !0 = $request, !1 = $method, !2 = $path, !3 = $handler, !4 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        INIT_FCALL                                               'strtolower'
          2        INIT_METHOD_CALL                                         !0, 'getMethod'
          3        DO_FCALL                                      0  $5      
          4        SEND_VAR                                                 $5
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !1, $6
   43     7        FETCH_OBJ_IS                                     ~8      'routes'
          8        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      ~8, !1
          9        BOOL_NOT                                         ~10     ~9
         10      > JMPZ                                                     ~10, ->12
   44    11    > > RETURN                                                   <false>
   47    12    >   INIT_METHOD_CALL                                         !0, 'getPath'
         13        DO_FCALL                                      0  $11     
         14        ASSIGN                                                   !2, $11
   48    15        FETCH_OBJ_R                                      ~13     'routes'
         16        FETCH_DIM_R                                      ~14     ~13, !1
         17      > FE_RESET_R                                       $15     ~14, ->25
         18    > > FE_FETCH_R                                       ~16     $15, !3, ->25
         19    >   ASSIGN                                                   !4, ~16
   49    20        IS_IDENTICAL                                             !4, !2
         21      > JMPZ                                                     ~18, ->24
   50    22    >   FE_FREE                                                  $15
         23      > RETURN                                                   !3
   48    24    > > JMP                                                      ->18
         25    >   FE_FREE                                                  $15
   54    26      > RETURN                                                   <false>
   55    27*     > RETURN                                                   null

End of function match

End of class Router.

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

End of function __construct

Function handle:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TgBhJ
function name:  handle
number of ops:  13
compiled vars:  !0 = $request, !1 = $handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
   68     1        FETCH_OBJ_R                                      ~2      'router'
          2        INIT_METHOD_CALL                                         ~2, 'match'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
   69     6        BOOL_NOT                                         ~5      !1
          7      > JMPZ                                                     ~5, ->10
   70     8    >   ECHO                                                     'Could+not+find+your+resource%21%0A'
   71     9      > RETURN                                                   null
   74    10    >   INIT_DYNAMIC_CALL                                        !1
         11        DO_FCALL                                      0          
   75    12      > RETURN                                                   null

End of function handle

End of class Dispatcher.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.16 ms | 1408 KiB | 15 Q