<?php <?php interface math{ public function calc($op1,$op2); } class mathadd implements math{ public function calc($op1,$op2){ return $op1+$op2; } } class mathsub implements math{ public function calc($op1,$op2){ return $op1-$op2; } } class mathmul implements math{ public function calc($op1,$op2){ return $op1*$op2; } } class mathdiv implements math{ public function calc($op1,$op2){ return $op1/$op2; } } class cmath{ protected $calc = null; public function __construct($type){ $calc = "math".$type; $this->calc = new $calc(); } public function calc($op1,$op2){ return $this->calc->calc($op1,$op2); } } $type = $_POST['op']; $cmath = new cmath($type); echo $cmath->calc($op1, $op2);
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/8691472.html