interface IMethod
{
function calculate($oprrand1, $oprrand2);
}
calss Multiplication implements IMethod{
function calculate($oprrand1, $oprrand2) {
return $oprrand1 * $oprrand2;
}
}
class Division implements IMethod{
function calculate($oprrand1, $oprrand2) {
if(oprrand2 == 0)
throw new Exception('Деление на ноль - невозможно.');
return $oprrand1 / $oprrand2;
}
}
class Operation {
protected $_method
function __construct (IMethod $method) {
$this->_method = $method;
}
function write($oprrand1, $oprrand2) {
echo "Result ".$this->_method->calculate($oprrand1, $oprrand2);
}
}
$operation1 = new Operation(new Division);
$operation2 = new Operation(new Division);
$operation1->write(4,2);
$operation2->write(4,2);