Complete the code to declare the Strategy interface.
<?php
interface [1] {
public function execute();
}
?>The interface that defines the strategy method is named Strategy.
Complete the code to implement a concrete strategy class.
<?php class ConcreteStrategyA implements [1] { public function execute() { echo "Strategy A executed."; } } ?>
The concrete strategy class must implement the Strategy interface.
Fix the error in the Context class constructor to accept a strategy.
<?php class Context { private $strategy; public function __construct([1] $strategy) { $this->strategy = $strategy; } } ?>
The constructor should accept a parameter typed as the Strategy interface to allow any strategy implementation.
Complete the code to call the strategy's execute method inside Context.
<?php class Context { private $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function executeStrategy() { $this->strategy->execute[1]; } } ?>
To call a method on an object in PHP, use the -> operator followed by parentheses () for the method call.
Fill all three blanks to create a Context with a ConcreteStrategyA and execute it.
<?php $strategy = new [1](); $context = new Context([2]); $context->[3](); ?>
First, create a new ConcreteStrategyA object, then pass it to the Context constructor, and finally call executeStrategy() on the context.