3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StrategyContext { private $strategy = NULL; //bookList is not instantiated at construct time public function __construct($strategy_ind_id) { switch ($strategy_ind_id) { case "C": $this->strategy = new StrategyCaps(); break; case "E": $this->strategy = new StrategyExclaim(); break; case "S": $this->strategy = new StrategyStars(); break; } } public function showBookTitle($book) { return $this->strategy->showTitle($book); } } interface StrategyInterface { public function showTitle($book_in); } class StrategyCaps implements StrategyInterface { public function showTitle($book_in) { $title = $book_in->getTitle(); $this->titleCount++; return strtoupper ($title); } } class StrategyExclaim implements StrategyInterface { public function showTitle($book_in) { $title = $book_in->getTitle(); $this->titleCount++; return Str_replace(' ','!',$title); } } class StrategyStars implements StrategyInterface { public function showTitle($book_in) { $title = $book_in->getTitle(); $this->titleCount++; return Str_replace(' ','*',$title); } } class Book { private $author; private $title; function __construct($title_in, $author_in) { $this->author = $author_in; $this->title = $title_in; } function getAuthor() { return $this->author; } function getTitle() { return $this->title; } function getAuthorAndTitle() { return $this->getTitle() . ' by ' . $this->getAuthor(); } } writeln('BEGIN TESTING STRATEGY PATTERN'); writeln(''); $book = new Book('PHP for Cats','Larry Truett'); $strategyContextC = new StrategyContext('C'); $strategyContextE = new StrategyContext('E'); $strategyContextS = new StrategyContext('S'); writeln('test 1 - show name context C'); writeln($strategyContextC->showBookTitle($book)); writeln(''); writeln('test 2 - show name context E'); writeln($strategyContextE->showBookTitle($book)); writeln(''); writeln('test 3 - show name context S'); writeln($strategyContextS->showBookTitle($book)); writeln(''); writeln('END TESTING STRATEGY PATTERN'); function writeln($line_in) { echo $line_in."<br/>"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  (null)
number of ops:  66
compiled vars:  !0 = $book, !1 = $strategyContextC, !2 = $strategyContextE, !3 = $strategyContextS
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   DECLARE_CLASS                                            'strategycaps'
   36     1        DECLARE_CLASS                                            'strategyexclaim'
   44     2        DECLARE_CLASS                                            'strategystars'
   70     3        INIT_FCALL_BY_NAME                                       'writeln'
          4        SEND_VAL_EX                                              'BEGIN+TESTING+STRATEGY+PATTERN'
          5        DO_FCALL                                      0          
   71     6        INIT_FCALL_BY_NAME                                       'writeln'
          7        SEND_VAL_EX                                              ''
          8        DO_FCALL                                      0          
   73     9        NEW                                              $6      'Book'
         10        SEND_VAL_EX                                              'PHP+for+Cats'
         11        SEND_VAL_EX                                              'Larry+Truett'
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !0, $6
   75    14        NEW                                              $9      'StrategyContext'
         15        SEND_VAL_EX                                              'C'
         16        DO_FCALL                                      0          
         17        ASSIGN                                                   !1, $9
   76    18        NEW                                              $12     'StrategyContext'
         19        SEND_VAL_EX                                              'E'
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !2, $12
   77    22        NEW                                              $15     'StrategyContext'
         23        SEND_VAL_EX                                              'S'
         24        DO_FCALL                                      0          
         25        ASSIGN                                                   !3, $15
   79    26        INIT_FCALL_BY_NAME                                       'writeln'
         27        SEND_VAL_EX                                              'test+1+-+show+name+context+C'
         28        DO_FCALL                                      0          
   80    29        INIT_FCALL_BY_NAME                                       'writeln'
         30        INIT_METHOD_CALL                                         !1, 'showBookTitle'
         31        SEND_VAR_EX                                              !0
         32        DO_FCALL                                      0  $19     
         33        SEND_VAR_NO_REF_EX                                       $19
         34        DO_FCALL                                      0          
   81    35        INIT_FCALL_BY_NAME                                       'writeln'
         36        SEND_VAL_EX                                              ''
         37        DO_FCALL                                      0          
   83    38        INIT_FCALL_BY_NAME                                       'writeln'
         39        SEND_VAL_EX                                              'test+2+-+show+name+context+E'
         40        DO_FCALL                                      0          
   84    41        INIT_FCALL_BY_NAME                                       'writeln'
         42        INIT_METHOD_CALL                                         !2, 'showBookTitle'
         43        SEND_VAR_EX                                              !0
         44        DO_FCALL                                      0  $23     
         45        SEND_VAR_NO_REF_EX                                       $23
         46        DO_FCALL                                      0          
   85    47        INIT_FCALL_BY_NAME                                       'writeln'
         48        SEND_VAL_EX                                              ''
         49        DO_FCALL                                      0          
   87    50        INIT_FCALL_BY_NAME                                       'writeln'
         51        SEND_VAL_EX                                              'test+3+-+show+name+context+S'
         52        DO_FCALL                                      0          
   88    53        INIT_FCALL_BY_NAME                                       'writeln'
         54        INIT_METHOD_CALL                                         !3, 'showBookTitle'
         55        SEND_VAR_EX                                              !0
         56        DO_FCALL                                      0  $27     
         57        SEND_VAR_NO_REF_EX                                       $27
         58        DO_FCALL                                      0          
   89    59        INIT_FCALL_BY_NAME                                       'writeln'
         60        SEND_VAL_EX                                              ''
         61        DO_FCALL                                      0          
   91    62        INIT_FCALL_BY_NAME                                       'writeln'
         63        SEND_VAL_EX                                              'END+TESTING+STRATEGY+PATTERN'
         64        DO_FCALL                                      0          
   95    65      > RETURN                                                   1

Function writeln:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  writeln
number of ops:  4
compiled vars:  !0 = $line_in
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   93     0  E >   RECV                                             !0      
   94     1        CONCAT                                           ~1      !0, '%3Cbr%2F%3E'
          2        ECHO                                                     ~1
   95     3      > RETURN                                                   null

End of function writeln

Class StrategyContext:
Function __construct:
Finding entry points
Branch analysis from position: 0
5 jumps found. (Code = 188) Position 1 = 9, Position 2 = 14, Position 3 = 19, Position 4 = 24, Position 5 = 2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 24
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 19
Branch analysis from position: 14
Branch analysis from position: 9
filename:       /in/WfGkY
function name:  __construct
number of ops:  25
compiled vars:  !0 = $strategy_ind_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1      > SWITCH_STRING                                            !0, [ 'C':->9, 'E':->14, 'S':->19, ], ->24
    8     2    >   IS_EQUAL                                                 !0, 'C'
          3      > JMPNZ                                                    ~1, ->9
   11     4    >   IS_EQUAL                                                 !0, 'E'
          5      > JMPNZ                                                    ~1, ->14
   14     6    >   IS_EQUAL                                                 !0, 'S'
          7      > JMPNZ                                                    ~1, ->19
          8    > > JMP                                                      ->24
    9     9    >   NEW                                              $3      'StrategyCaps'
         10        DO_FCALL                                      0          
         11        ASSIGN_OBJ                                               'strategy'
         12        OP_DATA                                                  $3
   10    13      > JMP                                                      ->24
   12    14    >   NEW                                              $6      'StrategyExclaim'
         15        DO_FCALL                                      0          
         16        ASSIGN_OBJ                                               'strategy'
         17        OP_DATA                                                  $6
   13    18      > JMP                                                      ->24
   15    19    >   NEW                                              $9      'StrategyStars'
         20        DO_FCALL                                      0          
         21        ASSIGN_OBJ                                               'strategy'
         22        OP_DATA                                                  $9
   16    23      > JMP                                                      ->24
   18    24    > > RETURN                                                   null

End of function __construct

Function showbooktitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  showBookTitle
number of ops:  7
compiled vars:  !0 = $book
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        FETCH_OBJ_R                                      ~1      'strategy'
          2        INIT_METHOD_CALL                                         ~1, 'showTitle'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5      > RETURN                                                   $2
   21     6*     > RETURN                                                   null

End of function showbooktitle

End of class StrategyContext.

Class StrategyInterface:
Function showtitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  showTitle
number of ops:  2
compiled vars:  !0 = $book_in
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function showtitle

End of class StrategyInterface.

Class StrategyCaps:
Function showtitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  showTitle
number of ops:  10
compiled vars:  !0 = $book_in, !1 = $title
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        INIT_METHOD_CALL                                         !0, 'getTitle'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
   31     4        PRE_INC_OBJ                                              'titleCount'
   32     5        INIT_FCALL                                               'strtoupper'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $5      
          8      > RETURN                                                   $5
   33     9*     > RETURN                                                   null

End of function showtitle

End of class StrategyCaps.

Class StrategyExclaim:
Function showtitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  showTitle
number of ops:  12
compiled vars:  !0 = $book_in, !1 = $title
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1        INIT_METHOD_CALL                                         !0, 'getTitle'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
   39     4        PRE_INC_OBJ                                              'titleCount'
   40     5        INIT_FCALL                                               'str_replace'
          6        SEND_VAL                                                 '+'
          7        SEND_VAL                                                 '%21'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $5      
         10      > RETURN                                                   $5
   41    11*     > RETURN                                                   null

End of function showtitle

End of class StrategyExclaim.

Class StrategyStars:
Function showtitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  showTitle
number of ops:  12
compiled vars:  !0 = $book_in, !1 = $title
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   46     1        INIT_METHOD_CALL                                         !0, 'getTitle'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
   47     4        PRE_INC_OBJ                                              'titleCount'
   48     5        INIT_FCALL                                               'str_replace'
          6        SEND_VAL                                                 '+'
          7        SEND_VAL                                                 '%2A'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $5      
         10      > RETURN                                                   $5
   49    11*     > RETURN                                                   null

End of function showtitle

End of class StrategyStars.

Class Book:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  __construct
number of ops:  7
compiled vars:  !0 = $title_in, !1 = $author_in
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   56     2        ASSIGN_OBJ                                               'author'
          3        OP_DATA                                                  !1
   57     4        ASSIGN_OBJ                                               'title'
          5        OP_DATA                                                  !0
   58     6      > RETURN                                                   null

End of function __construct

Function getauthor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  getAuthor
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   FETCH_OBJ_R                                      ~0      'author'
          1      > RETURN                                                   ~0
   61     2*     > RETURN                                                   null

End of function getauthor

Function gettitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  getTitle
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   FETCH_OBJ_R                                      ~0      'title'
          1      > RETURN                                                   ~0
   64     2*     > RETURN                                                   null

End of function gettitle

Function getauthorandtitle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WfGkY
function name:  getAuthorAndTitle
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   INIT_METHOD_CALL                                         'getTitle'
          1        DO_FCALL                                      0  $0      
          2        CONCAT                                           ~1      $0, '+by+'
          3        INIT_METHOD_CALL                                         'getAuthor'
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      ~1, $2
          6      > RETURN                                                   ~3
   67     7*     > RETURN                                                   null

End of function getauthorandtitle

End of class Book.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.18 ms | 1404 KiB | 17 Q