3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); ini_set('display_errors', 1); class system_tags { function __call($name, $params) { return '(METHOD "' . $name . '" WAS CALLED)'; } } class App { function run() { // Prerequisites. $system_tags = new system_tags(); $core = new \stdClass(); $core->tags = array( 'hello' => 'tagHelloMethod', 'world' => 'tagWorldMethod', ); // Approach 1. $this->template = 'Blah blah {hello} blah blah {world}.'; $this->withStrReplace($core, $system_tags); echo $this->template . "\n"; // Approach 2. $this->template = 'Blah blah {hello} blah blah {world}.'; $this->withRegexReplaceCallback($core, $system_tags); echo $this->template . "\n"; } function withStrReplace($core, $system_tags) { foreach($core->tags as $tag_name => $tag_method) { $this->template = str_replace( '{' . $tag_name . '}', $system_tags->{$tag_method}(), $this->template ); } } function withRegexReplaceCallback($core, $system_tags) { $callback = static function ($match) use ($system_tags) { return $system_tags->{$match['method_name']}(); }; foreach($core->tags as $tag_name => $tag_method) { $this->template = preg_replace_callback( '/\{(?<method_name>' . preg_quote($tag_name) . ')\}/', $callback, $this->template ); } } } $app = new App(); $app->run();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u4rgr
function name:  (null)
number of ops:  13
compiled vars:  !0 = $app
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
    3     3        INIT_FCALL                                               'ini_set'
          4        SEND_VAL                                                 'display_errors'
          5        SEND_VAL                                                 1
          6        DO_ICALL                                                 
   62     7        NEW                                              $3      'App'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $3
   63    10        INIT_METHOD_CALL                                         !0, 'run'
         11        DO_FCALL                                      0          
         12      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2Fu4rgr%3A47%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u4rgr
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $match, !1 = $system_tags
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   48     2        FETCH_DIM_R                                      ~2      !0, 'method_name'
          3        INIT_METHOD_CALL                                         !1, ~2
          4        DO_FCALL                                      0  $3      
          5      > RETURN                                                   $3
   49     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fu4rgr%3A47%240

Class system_tags:
Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u4rgr
function name:  __call
number of ops:  6
compiled vars:  !0 = $name, !1 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        CONCAT                                           ~2      '%28METHOD+%22', !0
          3        CONCAT                                           ~3      ~2, '%22+WAS+CALLED%29'
          4      > RETURN                                                   ~3
    8     5*     > RETURN                                                   null

End of function __call

End of class system_tags.

Class App:
Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u4rgr
function name:  run
number of ops:  27
compiled vars:  !0 = $system_tags, !1 = $core
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   NEW                                              $2      'system_tags'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   16     3        NEW                                              $5      'stdClass'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $5
   17     6        ASSIGN_OBJ                                               !1, 'tags'
   18     7        OP_DATA                                                  <array>
   23     8        ASSIGN_OBJ                                               'template'
          9        OP_DATA                                                  'Blah+blah+%7Bhello%7D+blah+blah+%7Bworld%7D.'
   24    10        INIT_METHOD_CALL                                         'withStrReplace'
         11        SEND_VAR_EX                                              !1
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
   25    14        FETCH_OBJ_R                                      ~11     'template'
         15        CONCAT                                           ~12     ~11, '%0A'
         16        ECHO                                                     ~12
   28    17        ASSIGN_OBJ                                               'template'
         18        OP_DATA                                                  'Blah+blah+%7Bhello%7D+blah+blah+%7Bworld%7D.'
   29    19        INIT_METHOD_CALL                                         'withRegexReplaceCallback'
         20        SEND_VAR_EX                                              !1
         21        SEND_VAR_EX                                              !0
         22        DO_FCALL                                      0          
   30    23        FETCH_OBJ_R                                      ~15     'template'
         24        CONCAT                                           ~16     ~15, '%0A'
         25        ECHO                                                     ~16
   31    26      > RETURN                                                   null

End of function run

Function withstrreplace:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/u4rgr
function name:  withStrReplace
number of ops:  21
compiled vars:  !0 = $core, !1 = $system_tags, !2 = $tag_method, !3 = $tag_name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   35     2        FETCH_OBJ_R                                      ~4      !0, 'tags'
          3      > FE_RESET_R                                       $5      ~4, ->19
          4    > > FE_FETCH_R                                       ~6      $5, !2, ->19
          5    >   ASSIGN                                                   !3, ~6
   36     6        INIT_FCALL                                               'str_replace'
   37     7        CONCAT                                           ~9      '%7B', !3
          8        CONCAT                                           ~10     ~9, '%7D'
          9        SEND_VAL                                                 ~10
   38    10        INIT_METHOD_CALL                                         !1, !2
         11        DO_FCALL                                      0  $11     
         12        SEND_VAR                                                 $11
   39    13        FETCH_OBJ_R                                      ~12     'template'
         14        SEND_VAL                                                 ~12
         15        DO_ICALL                                         $13     
   36    16        ASSIGN_OBJ                                               'template'
   39    17        OP_DATA                                                  $13
   35    18      > JMP                                                      ->4
         19    >   FE_FREE                                                  $5
   43    20      > RETURN                                                   null

End of function withstrreplace

Function withregexreplacecallback:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 23
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/u4rgr
function name:  withRegexReplaceCallback
number of ops:  25
compiled vars:  !0 = $core, !1 = $system_tags, !2 = $callback, !3 = $tag_method, !4 = $tag_name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fu4rgr%3A47%240'
          3        BIND_LEXICAL                                             ~5, !1
          4        ASSIGN                                                   !2, ~5
   51     5        FETCH_OBJ_R                                      ~7      !0, 'tags'
          6      > FE_RESET_R                                       $8      ~7, ->23
          7    > > FE_FETCH_R                                       ~9      $8, !3, ->23
          8    >   ASSIGN                                                   !4, ~9
   52     9        INIT_FCALL                                               'preg_replace_callback'
   53    10        INIT_FCALL                                               'preg_quote'
         11        SEND_VAR                                                 !4
         12        DO_ICALL                                         $12     
         13        CONCAT                                           ~13     '%2F%5C%7B%28%3F%3Cmethod_name%3E', $12
         14        CONCAT                                           ~14     ~13, '%29%5C%7D%2F'
         15        SEND_VAL                                                 ~14
   54    16        SEND_VAR                                                 !2
   55    17        FETCH_OBJ_R                                      ~15     'template'
         18        SEND_VAL                                                 ~15
         19        DO_ICALL                                         $16     
   52    20        ASSIGN_OBJ                                               'template'
   55    21        OP_DATA                                                  $16
   51    22      > JMP                                                      ->7
         23    >   FE_FREE                                                  $8
   59    24      > RETURN                                                   null

End of function withregexreplacecallback

End of class App.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
198.27 ms | 1396 KiB | 23 Q