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); $dispatcher->handle(new Request('GET', 'foo')); $dispatcher->handle(new Request('POST', 'bar')); $dispatcher->handle(new Request('GET', 'qux'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gbsoJ
function name:  (null)
number of ops:  39
compiled vars:  !0 = $router, !1 = $dispatcher
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   NEW                                              $2      'Router'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   80     3        INIT_METHOD_CALL                                         !0, 'get'
          4        SEND_VAL_EX                                              'foo'
          5        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
          6        SEND_VAL_EX                                              ~5
          7        DO_FCALL                                      0          
   81     8        INIT_METHOD_CALL                                         !0, 'post'
          9        SEND_VAL_EX                                              'bar'
         10        DECLARE_LAMBDA_FUNCTION                          ~7      [1]
         11        SEND_VAL_EX                                              ~7
         12        DO_FCALL                                      0          
   83    13        NEW                                              $9      'Dispatcher'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $9
   85    17        INIT_METHOD_CALL                                         !1, 'handle'
         18        NEW                                              $12     'Request'
         19        SEND_VAL_EX                                              'GET'
         20        SEND_VAL_EX                                              'foo'
         21        DO_FCALL                                      0          
         22        SEND_VAR_NO_REF_EX                                       $12
         23        DO_FCALL                                      0          
   86    24        INIT_METHOD_CALL                                         !1, 'handle'
         25        NEW                                              $15     'Request'
         26        SEND_VAL_EX                                              'POST'
         27        SEND_VAL_EX                                              'bar'
         28        DO_FCALL                                      0          
         29        SEND_VAR_NO_REF_EX                                       $15
         30        DO_FCALL                                      0          
   87    31        INIT_METHOD_CALL                                         !1, 'handle'
         32        NEW                                              $18     'Request'
         33        SEND_VAL_EX                                              'GET'
         34        SEND_VAL_EX                                              'qux'
         35        DO_FCALL                                      0          
         36        SEND_VAR_NO_REF_EX                                       $18
         37        DO_FCALL                                      0          
         38      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gbsoJ
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 Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gbsoJ
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 Dynamic Function 1

Class Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gbsoJ
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/gbsoJ
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/gbsoJ
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/gbsoJ
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/gbsoJ
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/gbsoJ
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/gbsoJ
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/gbsoJ
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:
142.31 ms | 1456 KiB | 14 Q