3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TemplateString { // Usage: // $t = new TemplateString("Your name is {{name}}"); // $t->name = "John"; // echo $t; // Your name is John private $template; private $parameters = []; public function __construct( $template, array $defaultparams = null) { $this->template = $template; if( $defaultparams) $this->parameters = $defaultparams; } public function __set($k,$v) { $this->parameters[$k] = $v; } public function __get($k) { if( array_key_exists($k,$this->parameters)) { return $this->parameters[$k]; } return null; } public function __toString() { $words = $this->parameters; return preg_replace_callback("/\{\{(\w+)\}\}/",function($m) use ($words) { if( array_key_exists($m[1],$words)) return $words[$m[1]]; return $m[0]; },$this->template); } } $t = new TemplateString("Your name is {{name}}\n"); $t->name = "John"; echo $t; $t->name = "Bill"; echo $t; // alternative: $t2 = clone $t; $t2->name = "Sam"; echo $t.$t2;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vNhYl
function name:  (null)
number of ops:  18
compiled vars:  !0 = $t, !1 = $t2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'templatestring'
   33     1        NEW                                              $2      'TemplateString'
          2        SEND_VAL_EX                                              'Your+name+is+%7B%7Bname%7D%7D%0A'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
   34     5        ASSIGN_OBJ                                               !0, 'name'
          6        OP_DATA                                                  'John'
   35     7        ECHO                                                     !0
   36     8        ASSIGN_OBJ                                               !0, 'name'
          9        OP_DATA                                                  'Bill'
   37    10        ECHO                                                     !0
   40    11        CLONE                                            ~7      !0
         12        ASSIGN                                                   !1, ~7
   41    13        ASSIGN_OBJ                                               !1, 'name'
         14        OP_DATA                                                  'Sam'
   42    15        CONCAT                                           ~10     !0, !1
         16        ECHO                                                     ~10
         17      > RETURN                                                   1

Class TemplateString:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/vNhYl
function name:  __construct
number of ops:  8
compiled vars:  !0 = $template, !1 = $defaultparams
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   12     2        ASSIGN_OBJ                                               'template'
          3        OP_DATA                                                  !0
   13     4      > JMPZ                                                     !1, ->7
          5    >   ASSIGN_OBJ                                               'parameters'
          6        OP_DATA                                                  !1
   14     7    > > RETURN                                                   null

End of function __construct

Function __set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vNhYl
function name:  __set
number of ops:  6
compiled vars:  !0 = $k, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        FETCH_OBJ_W                                      $2      'parameters'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   17     5      > RETURN                                                   null

End of function __set

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vNhYl
function name:  __get
number of ops:  9
compiled vars:  !0 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   19     1        FETCH_OBJ_R                                      ~1      'parameters'
          2        ARRAY_KEY_EXISTS                                         !0, ~1
          3      > JMPZ                                                     ~2, ->7
   20     4    >   FETCH_OBJ_R                                      ~3      'parameters'
          5        FETCH_DIM_R                                      ~4      ~3, !0
          6      > RETURN                                                   ~4
   22     7    > > RETURN                                                   null
   23     8*     > RETURN                                                   null

End of function __get

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vNhYl
function name:  __toString
number of ops:  14
compiled vars:  !0 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_OBJ_R                                      ~1      'parameters'
          1        ASSIGN                                                   !0, ~1
   26     2        INIT_FCALL                                               'preg_replace_callback'
          3        SEND_VAL                                                 '%2F%5C%7B%5C%7B%28%5Cw%2B%29%5C%7D%5C%7D%2F'
          4        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
          5        BIND_LEXICAL                                             ~3, !0
   29     6        SEND_VAL                                                 ~3
          7        FETCH_OBJ_R                                      ~4      'template'
          8        SEND_VAL                                                 ~4
   26     9        DO_ICALL                                         $5      
   29    10        VERIFY_RETURN_TYPE                                       $5
         11      > RETURN                                                   $5
   30    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vNhYl
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $m, !1 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   27     2        FETCH_DIM_R                                      ~2      !0, 1
          3        ARRAY_KEY_EXISTS                                         ~2, !1
          4      > JMPZ                                                     ~3, ->8
          5    >   FETCH_DIM_R                                      ~4      !0, 1
          6        FETCH_DIM_R                                      ~5      !1, ~4
          7      > RETURN                                                   ~5
   28     8    >   FETCH_DIM_R                                      ~6      !0, 0
          9      > RETURN                                                   ~6
   29    10*     > RETURN                                                   null

End of Dynamic Function 0

End of function __tostring

End of class TemplateString.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.17 ms | 1019 KiB | 14 Q