3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Thesaurus { protected $synonyms = array( array('wonderful', 'great', 'amazing', 'fabulous'), array('look', 'see', 'observe') ); public function replace($word) { foreach ($this->synonyms as $synonym) { if (in_array($word, $synonym)) { return $synonym[ array_rand($synonym) ]; } } return $word; } } class Transformer { protected $thesaurus; public function __construct(Thesaurus $thesaurus) { $this->thesaurus = $thesaurus; } public function transform($string) { foreach (str_word_count($string, 1) as $word) { $string = str_replace($word, $this->thesaurus->replace($word), $string); } return $string; } } $transformer = new Transformer(new Thesaurus); foreach (range(1, 20) as $index) { echo $transformer->transform('This is my wonderful description. It really is wonderful. Look how wonderful it is.'), PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/ceE0C
function name:  (null)
number of ops:  20
compiled vars:  !0 = $transformer, !1 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   NEW                                              $2      'Transformer'
          1        NEW                                              $3      'Thesaurus'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   35     6        INIT_FCALL                                               'range'
          7        SEND_VAL                                                 1
          8        SEND_VAL                                                 20
          9        DO_ICALL                                         $7      
         10      > FE_RESET_R                                       $8      $7, ->18
         11    > > FE_FETCH_R                                               $8, !1, ->18
   36    12    >   INIT_METHOD_CALL                                         !0, 'transform'
         13        SEND_VAL_EX                                              'This+is+my+wonderful+description.+It+really+is+wonderful.+Look+how+wonderful+it+is.'
         14        DO_FCALL                                      0  $9      
         15        ECHO                                                     $9
         16        ECHO                                                     '%0A'
   35    17      > JMP                                                      ->11
         18    >   FE_FREE                                                  $8
   37    19      > RETURN                                                   1

Class Thesaurus:
Function replace:
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 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/ceE0C
function name:  replace
number of ops:  19
compiled vars:  !0 = $word, !1 = $synonym
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        FETCH_OBJ_R                                      ~2      'synonyms'
          2      > FE_RESET_R                                       $3      ~2, ->16
          3    > > FE_FETCH_R                                               $3, !1, ->16
   11     4    >   INIT_FCALL                                               'in_array'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8      > JMPZ                                                     $4, ->15
   12     9    >   INIT_FCALL                                               'array_rand'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $5      
         12        FETCH_DIM_R                                      ~6      !1, $5
         13        FE_FREE                                                  $3
         14      > RETURN                                                   ~6
   10    15    > > JMP                                                      ->3
         16    >   FE_FREE                                                  $3
   15    17      > RETURN                                                   !0
   16    18*     > RETURN                                                   null

End of function replace

End of class Thesaurus.

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

End of function __construct

Function transform:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/ceE0C
function name:  transform
number of ops:  21
compiled vars:  !0 = $string, !1 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   26     1        INIT_FCALL                                               'str_word_count'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 1
          4        DO_ICALL                                         $2      
          5      > FE_RESET_R                                       $3      $2, ->18
          6    > > FE_FETCH_R                                               $3, !1, ->18
   27     7    >   INIT_FCALL                                               'str_replace'
          8        SEND_VAR                                                 !1
          9        FETCH_OBJ_R                                      ~4      'thesaurus'
         10        INIT_METHOD_CALL                                         ~4, 'replace'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $5      
         13        SEND_VAR                                                 $5
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $6      
         16        ASSIGN                                                   !0, $6
   26    17      > JMP                                                      ->6
         18    >   FE_FREE                                                  $3
   29    19      > RETURN                                                   !0
   30    20*     > RETURN                                                   null

End of function transform

End of class Transformer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.08 ms | 1404 KiB | 23 Q