1. 创建主程序test.php:基本的表单和通过post提交输入值进行调用对象中的方法,计算出最后的值并输出
<table width="500" align="center" border="0">
<form action="test.php" method="post">
<tr>
<td>第一个输入框:</td>
<td><input type='text' name='text1'></td>
</tr>
<tr>
<td>第二个输入框:</td>
<td><input type='text' name='text2'></td>
</tr>
<tr align='center'>
<td colspan='2'><input type='submit' name='sub' value='计算'><td>
</tr>
<tr>
<td>
<?php
if(isset($_POST['sub'])&&($_POST['text1']!=='')&&($_POST['text2']!=='')){
include "statics.php";
//创建对象
$stat=new new_statics;
//调用对象中的方法并传参;
$value=$stat->sum($_POST['text1'],$_POST['text2']);
echo "计算后的结果为:".$value;
}
?>
</td>
</tr>
</form>
</table>
2.创建一个类,再创建一个子类重载其中的方法
<?php
//父类statics
class statics{
function sum($var1,$var2){
return intval($var1+$var2);
}
}
//子类new_statics
class new_statics extends statics{
function sum($var1,$var2){
if(is_numeric($var1)&&is_numeric($var2)){
$value1=intval($var1+$var2);
return $value1;
}
if(!is_numeric($var1)||!is_numeric($var2)){
$value2=$var1.$var2;
return $value2;
}
}
}
?>
原文链接:https://blog.csdn.net/living_ren/article/details/75365374
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~