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/>"; }
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
BEGIN TESTING STRATEGY PATTERN<br/><br/>test 1 - show name context C<br/> Deprecated: Creation of dynamic property StrategyCaps::$titleCount is deprecated in /in/WfGkY on line 31 Warning: Undefined property: StrategyCaps::$titleCount in /in/WfGkY on line 31 PHP FOR CATS<br/><br/>test 2 - show name context E<br/> Deprecated: Creation of dynamic property StrategyExclaim::$titleCount is deprecated in /in/WfGkY on line 39 Warning: Undefined property: StrategyExclaim::$titleCount in /in/WfGkY on line 39 PHP!for!Cats<br/><br/>test 3 - show name context S<br/> Deprecated: Creation of dynamic property StrategyStars::$titleCount is deprecated in /in/WfGkY on line 47 Warning: Undefined property: StrategyStars::$titleCount in /in/WfGkY on line 47 PHP*for*Cats<br/><br/>END TESTING STRATEGY PATTERN<br/>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
BEGIN TESTING STRATEGY PATTERN<br/><br/>test 1 - show name context C<br/> Warning: Undefined property: StrategyCaps::$titleCount in /in/WfGkY on line 31 PHP FOR CATS<br/><br/>test 2 - show name context E<br/> Warning: Undefined property: StrategyExclaim::$titleCount in /in/WfGkY on line 39 PHP!for!Cats<br/><br/>test 3 - show name context S<br/> Warning: Undefined property: StrategyStars::$titleCount in /in/WfGkY on line 47 PHP*for*Cats<br/><br/>END TESTING STRATEGY PATTERN<br/>
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
BEGIN TESTING STRATEGY PATTERN<br/><br/>test 1 - show name context C<br/> Notice: Undefined property: StrategyCaps::$titleCount in /in/WfGkY on line 31 PHP FOR CATS<br/><br/>test 2 - show name context E<br/> Notice: Undefined property: StrategyExclaim::$titleCount in /in/WfGkY on line 39 PHP!for!Cats<br/><br/>test 3 - show name context S<br/> Notice: Undefined property: StrategyStars::$titleCount in /in/WfGkY on line 47 PHP*for*Cats<br/><br/>END TESTING STRATEGY PATTERN<br/>
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 7.3.32 - 7.3.33, 7.4.26
BEGIN TESTING STRATEGY PATTERN<br/><br/>test 1 - show name context C<br/>PHP FOR CATS<br/><br/>test 2 - show name context E<br/>PHP!for!Cats<br/><br/>test 3 - show name context S<br/>PHP*for*Cats<br/><br/>END TESTING STRATEGY PATTERN<br/>
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/WfGkY on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/WfGkY on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/WfGkY on line 4
Process exited with code 255.

preferences:
291.32 ms | 401 KiB | 458 Q