跟着教学视频做一个简单的图形计算器,基本上按着视频上面说的做,可是调用一个类之后,类中的方法找不到,求大神指点迷津。
<html>
<head>
<title>简单计算器</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<center>
<h1>简单计算器</h1>
<a href="calculator.php?action=rect">矩形</a>||
<a href="calculator.php?action=tri">三角形</a>
<hr><br>
<?php
//自动加载类文件
function __autoload($class){
include strtolower($class).".class.php";
}
//判断用户是否选择了一个图形链接
if(!empty($_GET['action'])){
//第一步,先选择一个图形链接
$class = ucfirst($_GET['action']);
$shape = new $class($_POST);
//第二步,调用shape对象中的view()
$shape->view();
//第三步,用户是否提交了对应图形界面的表单
if(isset($_POST['sub'])) {
//验证表单数据是否正确,不正确则提示
if($shape->test($_POST)) {
//开始计算图形的面积和周长
echo $shape->name . "的周长为:" . $shape->Perimeter() . "<br>";
echo $shape->name . "的面积为:" . $shape->area() . "<br>";
}
}
//如果用户没有选择图形链接,默认访问这个主程序
}else{
echo "请选择一个图形链接!<br>";
}
?>
</center>
</body>
</html>
下面是被调用的类:
<?php
class Rect extends Shape{
public $width;
public $height;
function __construct($arr=array()){
if(!empty($arr)){
$this->width = $arr['width'];
$this->height = $arr['height'];
}
$this->name = "矩形";
}
public function area(){
return $this->width * $this->height;
}
public function Perimeter(){
return 2*($this->width + $this->height);
}
public function view(){
$form = '<form action="calculator.php?action=rect" method="post">';
$form .= $this->name .'的宽:<input type="text" name="width" value="" /><br>';
$form .= $this->name .'的高:<input type="text" name="height" value="" /><br>';
$form .= '<input type="submit" name=sub" value="计算"> <br>';
$form .= '</form>';
echo $form;
}
public function test($arr){
$bj = true;
if($arr['width'] < 0){
echo $this->name."的宽不能小于0!<br>";
$bj = false;
}
if($arr['height'] < 0){
echo $this->name."的高不能小于0!<br>";
$bj = false;
}
return $bj;
}
}
看到的麻烦进来帮我看一下代码!!!谢谢!!!!!
<html>
<head>
<title>简单计算器</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<center>
<h1>简单计算器</h1>
<a href="calculator.php?action=rect">矩形</a>||
<a href="calculator.php?action=tri">三角形</a>
<hr><br>
<?php
//自动加载类文件
function __autoload($class){
include strtolower($class).".class.php";
}
//判断用户是否选择了一个图形链接
if(!empty($_GET['action'])){
//第一步,先选择一个图形链接
$class = ucfirst($_GET['action']);
$shape = new $class($_POST);
//第二步,调用shape对象中的view()
$shape->view();
//第三步,用户是否提交了对应图形界面的表单
if(isset($_POST['sub'])) {
//验证表单数据是否正确,不正确则提示
if($shape->test($_POST)) {
//开始计算图形的面积和周长
echo $shape->name . "的周长为:" . $shape->Perimeter() . "<br>";
echo $shape->name . "的面积为:" . $shape->area() . "<br>";
}
}
//如果用户没有选择图形链接,默认访问这个主程序
}else{
echo "请选择一个图形链接!<br>";
}
?>
</center>
</body>
</html>
下面是被调用的类:
<?php
class Rect extends Shape{
public $width;
public $height;
function __construct($arr=array()){
if(!empty($arr)){
$this->width = $arr['width'];
$this->height = $arr['height'];
}
$this->name = "矩形";
}
public function area(){
return $this->width * $this->height;
}
public function Perimeter(){
return 2*($this->width + $this->height);
}
public function view(){
$form = '<form action="calculator.php?action=rect" method="post">';
$form .= $this->name .'的宽:<input type="text" name="width" value="" /><br>';
$form .= $this->name .'的高:<input type="text" name="height" value="" /><br>';
$form .= '<input type="submit" name=sub" value="计算"> <br>';
$form .= '</form>';
echo $form;
}
public function test($arr){
$bj = true;
if($arr['width'] < 0){
echo $this->name."的宽不能小于0!<br>";
$bj = false;
}
if($arr['height'] < 0){
echo $this->name."的高不能小于0!<br>";
$bj = false;
}
return $bj;
}
}
看到的麻烦进来帮我看一下代码!!!谢谢!!!!!