3v4l.org

run code in 300+ PHP versions simultaneously
<?php $userSpecifiedPath = '/news'; // Change me! // Don't edit below =============================================== // ================================================================ // Simple config example // ================================================================ $routerConfig = <<<CONFIG [/] Controller = "HomepageController" [404] Controller = "404Controller" CONFIG; // ================================================================ // Simple router example // ================================================================ class Router { private $routes; public function __construct($routes) { $this->routes = $routes; } public function getRouteFromPath($path) { if(array_key_exists($path, $this->routes) === true) { return $this->routes[$path]; } elseif(array_key_exists('404', $this->routes) === true) { return $this->routes['404']; } throw new Exception('No controllers found.'); } public function getAllHierarchicalRoutes() { $routes = array(); foreach($this->routes as $key => $route) { if(strpos($key, '*') === strlen($key) - 1) { $routes[$key] = $route; } } return $routes; } public function getHierarchicalRouteFromPath($path) { foreach($this->routes as $key => $route) { if(strpos($key, '*') === strlen($key) - 1) { if(strpos($path, rtrim($key, '*')) === 0) { return $route; } } } } } // ================================================================ // Sample output // ================================================================ $routes = parse_ini_string($routerConfig, true); $Router = new Router($routes); $selectedRoute = $Router->getRouteFromPath($userSpecifiedPath); echo 'Using controller: ' . $selectedRoute['Controller'];
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VLNY3
function name:  (null)
number of ops:  19
compiled vars:  !0 = $userSpecifiedPath, !1 = $routerConfig, !2 = $routes, !3 = $Router, !4 = $selectedRoute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, '%2Fnews'
   13     1        ASSIGN                                                   !1, '%5B%2F%5D%0AController+%3D+%22HomepageController%22%0A%0A%5B404%5D%0AController+%3D+%22404Controller%22'
   70     2        INIT_FCALL                                               'parse_ini_string'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $7      
          6        ASSIGN                                                   !2, $7
   71     7        NEW                                              $9      'Router'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $9
   72    11        INIT_METHOD_CALL                                         !3, 'getRouteFromPath'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $12     
         14        ASSIGN                                                   !4, $12
   73    15        FETCH_DIM_R                                      ~14     !4, 'Controller'
         16        CONCAT                                           ~15     'Using+controller%3A+', ~14
         17        ECHO                                                     ~15
         18      > RETURN                                                   1

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

End of function __construct

Function getroutefrompath:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/VLNY3
function name:  getRouteFromPath
number of ops:  21
compiled vars:  !0 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        FETCH_OBJ_R                                      ~1      'routes'
          2        ARRAY_KEY_EXISTS                                 ~2      !0, ~1
          3        TYPE_CHECK                                    8          ~2
          4      > JMPZ                                                     ~3, ->9
   36     5    >   FETCH_OBJ_R                                      ~4      'routes'
          6        FETCH_DIM_R                                      ~5      ~4, !0
          7      > RETURN                                                   ~5
          8*       JMP                                                      ->16
   37     9    >   FETCH_OBJ_R                                      ~6      'routes'
         10        ARRAY_KEY_EXISTS                                 ~7      '404', ~6
         11        TYPE_CHECK                                    8          ~7
         12      > JMPZ                                                     ~8, ->16
   38    13    >   FETCH_OBJ_R                                      ~9      'routes'
         14        FETCH_DIM_R                                      ~10     ~9, 404
         15      > RETURN                                                   ~10
   40    16    >   NEW                                              $11     'Exception'
         17        SEND_VAL_EX                                              'No+controllers+found.'
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $11
   41    20*     > RETURN                                                   null

End of function getroutefrompath

Function getallhierarchicalroutes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/VLNY3
function name:  getAllHierarchicalRoutes
number of ops:  19
compiled vars:  !0 = $routes, !1 = $route, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, <array>
   45     1        FETCH_OBJ_R                                      ~4      'routes'
          2      > FE_RESET_R                                       $5      ~4, ->16
          3    > > FE_FETCH_R                                       ~6      $5, !1, ->16
          4    >   ASSIGN                                                   !2, ~6
   46     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !2
          7        SEND_VAL                                                 '%2A'
          8        DO_ICALL                                         $8      
          9        STRLEN                                           ~9      !2
         10        SUB                                              ~10     ~9, 1
         11        IS_IDENTICAL                                             $8, ~10
         12      > JMPZ                                                     ~11, ->15
   47    13    >   ASSIGN_DIM                                               !0, !2
         14        OP_DATA                                                  !1
   45    15    > > JMP                                                      ->3
         16    >   FE_FREE                                                  $5
   50    17      > RETURN                                                   !0
   51    18*     > RETURN                                                   null

End of function getallhierarchicalroutes

Function gethierarchicalroutefrompath:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 26
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 26
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 25
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 25
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/VLNY3
function name:  getHierarchicalRouteFromPath
number of ops:  28
compiled vars:  !0 = $path, !1 = $route, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
   54     1        FETCH_OBJ_R                                      ~3      'routes'
          2      > FE_RESET_R                                       $4      ~3, ->26
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->26
          4    >   ASSIGN                                                   !2, ~5
   55     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !2
          7        SEND_VAL                                                 '%2A'
          8        DO_ICALL                                         $7      
          9        STRLEN                                           ~8      !2
         10        SUB                                              ~9      ~8, 1
         11        IS_IDENTICAL                                             $7, ~9
         12      > JMPZ                                                     ~10, ->25
   56    13    >   INIT_FCALL                                               'strpos'
         14        SEND_VAR                                                 !0
         15        INIT_FCALL                                               'rtrim'
         16        SEND_VAR                                                 !2
         17        SEND_VAL                                                 '%2A'
         18        DO_ICALL                                         $11     
         19        SEND_VAR                                                 $11
         20        DO_ICALL                                         $12     
         21        IS_IDENTICAL                                             $12, 0
         22      > JMPZ                                                     ~13, ->25
   57    23    >   FE_FREE                                                  $4
         24      > RETURN                                                   !1
   54    25    > > JMP                                                      ->3
         26    >   FE_FREE                                                  $4
   61    27      > RETURN                                                   null

End of function gethierarchicalroutefrompath

End of class Router.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.6 ms | 1404 KiB | 19 Q