<?PHP//PHP Policy Mode Interfacemath{ Public functionCalc$op 1,$op 2); } classMathaddImplementsmath{ Public functionCalc$op 1,$op 2){ return $op 1+$op 2; } } classMathsubImplementsmath{ Public functionCalc$op 1,$op 2){ return $op 1-$op 2; } } classMathmulImplementsmath{ Public functionCalc$op 1,$op 2){ return $op 1*$op 2; } } classMathdivImplementsmath{ Public functionCalc$op 1,$op 2){ return $op 1/$op 2; } }//encapsulating a virtual machine classcmath{protected $calc=NULL; Public function__construct ($type){ $calc= ' Math '.$type; $this->calc=New $calc(); } Public functionCalc$op 1,$op 2){ return $this->calc->calc ($op 1,$op 2); } } $type=$_post[' Op ']; $cmath=NewCmath ($type); Echo $cmath->calc ($_post[' OP1 '],$_post[' OP2 ']);
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Policy mode</title></Head><Body> <formAction= ' 2.php 'Method= "POST"> <inputtype= "text"name= "OP1"> <Selectname= "Op"> <optionvalue= "Add">+</option> <optionvalue= "Sub">-</option> <optionvalue= "Mul">*</option> <optionvalue= "Div">/</option> </Select> <inputtype= "text"name= "OP2"> <P><inputtype= "Submit"name=""value= "Calculation"></P> </form></Body></HTML>
The strategy mode implements simple calculator function
PHP policy mode