<?php
//工厂模式
interface operstion{
function oper($a,$b);
}
//加
class add implements operstion{
function oper($a,$b){
echo $c = $a + $b;
}
}
//减
class subtract implements operstion{
function oper($a,$b){
echo $d = $b - $a;
}
}
//乘
class multiply implements operstion{
function oper($a,$b){
echo $e = $a * $b;
}
}
//除
class divide implements operstion{
function oper($a,$b){
$f = $b / $a;
}
}
class Factory{
static public function createDb($type){
switch($type){
case 'add':
return new add();
break;
case 'subtract':
return new subtract();
break;
case 'multiply':
return new multiply();
break;
case 'divide':
return new divide();
break;
}
}
}
$obj = Factory::createDb('add');
$obj->oper(2,4);