《细说php》第8章的最后一个例子。可以创建表单,但是点击计算之后出错误。请各位大侠指点迷津
接口:class__Shape.php
<?php
interface Shape{
function area();
function perimeter();
}
?>
矩形:class__Rect.php
<?php
class Rect implements Shape{
private $width;
private $length;
function __construct($size=""){
$this->width=$size["width"];
$this->length=$size["length"];
}
function area(){
return $this->length * $this->width;
}
function perimeter(){
return 2 * ($this->width + $this->length);
}
}
?>
三角形:class__Triangle.php
<?php
class Triangle implements Shape{
private $length1;
private $length2;
private $length3;
function __construct($size=""){
$this->length1 = $size["length1"];
$this->length2 = $size["length2"];
$this->length3 = $size["length3"];
}
function area(){
$s = ($this->length1 + $this->length2 + $this->length3)/2;
return sqrt($s * ($s - $this->length1) * ($s - $this->length2) * ($s - $this->length3));
}
function perimeter(){
return $this->length1 + $this->length2 + $this->length3;
}
}
?>
圆形:class__Circle.php
<?php
class Circle implements Shape{
private $radius;
function __construce($size=""){
$this->radius = $size["radius"];
}
function area(){
return pi() * $this->radius * $this->radius;
}
function perimeter(){
return 2* pi() * $this->radius;
}
}
?>
表格:class__Form.php
<?php
class Form{
private $formName;
private $request;
private $action;
private $method;
private $target;
function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="<table align=center border=5 width=800>";
$str.="<caption><h2>".$this->formName."</h2><caption>";
$str.="<form action=".$this->action."method=".$this->method."target=$this->target";
switch ($this->request["action"]){
case 1:
$str.="<tr><th>矩形长度:</th><td>";
$str.="<input type='text' name='length' value=".$this->request["length"]."></td></tr>";
$str.="<tr><th>矩形宽度:</th><td>";
$str.="<input type='text' name='width' value=".$this->request["width"]."></td></tr>";
break;
case 2:
$str.="<tr><th>三角形边长1:</th><td>";
$str.="<input type=text name='length1' value=".$this->request["length1"]."></td></tr>";
$str.="<tr><th>三角形边长2:</th><td>";
$str.="<input type=text name='length2' value=".$this->request["length2"]."></td></tr>";
$str.="<tr><th>三角形边长3:</th><td>";
$str.="<input type=text name='length3' value=".$this->request["length3"]."></td></tr>";
break;
case 3:
$str.="<tr><th>圆形半径:</th><td>";
$str.="<input type=text name='radius' value=".$this->request["radius"]."></td></tr>";
break;
}
$str.="<tr><td align=center colspan=2><input type=submit value='计算'></td></tr>";
$str.="<input type=hidden name='act' value=".$this->request["action"].">";
$str.="<input type=hidden name='action' value=".$this->request["action"].">";
$str.="</form></table>";
return $str;
}
}
?>
控制程序:index.php
<html>
<head>
<title>图形计算器</title>
</head>
<body>
<?php
function __autoload($className){
include ('class__'.ucfirst($className).'.php');
}
?>
<center>
<h2>图形周长和面积计算器
</h2><hr>
<a href="index.php?action=1">矩形</a>||
<a href="index.php?action=2">三角形</a>||
<a href="index.php?action=3">圆形</a><br />
</center>
<?php
switch ($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
default:
echo "请选择一个图形<br />";
}
if(isset($_REQUEST["act"])){
switch($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo "面积为:".$shape->area()."<br>";
echo "周长为:".$shape->perimeter()."<br>";
}
?>
</body>
</html>
接口:class__Shape.php
<?php
interface Shape{
function area();
function perimeter();
}
?>
矩形:class__Rect.php
<?php
class Rect implements Shape{
private $width;
private $length;
function __construct($size=""){
$this->width=$size["width"];
$this->length=$size["length"];
}
function area(){
return $this->length * $this->width;
}
function perimeter(){
return 2 * ($this->width + $this->length);
}
}
?>
三角形:class__Triangle.php
<?php
class Triangle implements Shape{
private $length1;
private $length2;
private $length3;
function __construct($size=""){
$this->length1 = $size["length1"];
$this->length2 = $size["length2"];
$this->length3 = $size["length3"];
}
function area(){
$s = ($this->length1 + $this->length2 + $this->length3)/2;
return sqrt($s * ($s - $this->length1) * ($s - $this->length2) * ($s - $this->length3));
}
function perimeter(){
return $this->length1 + $this->length2 + $this->length3;
}
}
?>
圆形:class__Circle.php
<?php
class Circle implements Shape{
private $radius;
function __construce($size=""){
$this->radius = $size["radius"];
}
function area(){
return pi() * $this->radius * $this->radius;
}
function perimeter(){
return 2* pi() * $this->radius;
}
}
?>
表格:class__Form.php
<?php
class Form{
private $formName;
private $request;
private $action;
private $method;
private $target;
function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="<table align=center border=5 width=800>";
$str.="<caption><h2>".$this->formName."</h2><caption>";
$str.="<form action=".$this->action."method=".$this->method."target=$this->target";
switch ($this->request["action"]){
case 1:
$str.="<tr><th>矩形长度:</th><td>";
$str.="<input type='text' name='length' value=".$this->request["length"]."></td></tr>";
$str.="<tr><th>矩形宽度:</th><td>";
$str.="<input type='text' name='width' value=".$this->request["width"]."></td></tr>";
break;
case 2:
$str.="<tr><th>三角形边长1:</th><td>";
$str.="<input type=text name='length1' value=".$this->request["length1"]."></td></tr>";
$str.="<tr><th>三角形边长2:</th><td>";
$str.="<input type=text name='length2' value=".$this->request["length2"]."></td></tr>";
$str.="<tr><th>三角形边长3:</th><td>";
$str.="<input type=text name='length3' value=".$this->request["length3"]."></td></tr>";
break;
case 3:
$str.="<tr><th>圆形半径:</th><td>";
$str.="<input type=text name='radius' value=".$this->request["radius"]."></td></tr>";
break;
}
$str.="<tr><td align=center colspan=2><input type=submit value='计算'></td></tr>";
$str.="<input type=hidden name='act' value=".$this->request["action"].">";
$str.="<input type=hidden name='action' value=".$this->request["action"].">";
$str.="</form></table>";
return $str;
}
}
?>
控制程序:index.php
<html>
<head>
<title>图形计算器</title>
</head>
<body>
<?php
function __autoload($className){
include ('class__'.ucfirst($className).'.php');
}
?>
<center>
<h2>图形周长和面积计算器
</h2><hr>
<a href="index.php?action=1">矩形</a>||
<a href="index.php?action=2">三角形</a>||
<a href="index.php?action=3">圆形</a><br />
</center>
<?php
switch ($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
default:
echo "请选择一个图形<br />";
}
if(isset($_REQUEST["act"])){
switch($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo "面积为:".$shape->area()."<br>";
echo "周长为:".$shape->perimeter()."<br>";
}
?>
</body>
</html>
$str.="<form action=".$this->action."method=".$this->method."target=$this->target";
应为
$str.="<form action=".$this->action." method=".$this->method." target=$this->target";
少抄了空格class__Circle.php 中
function __construce($size=""){
应为
function __construct($size=""){
抄错了一个字符
index.phpmethod=
改成这样:
index.php?method=
function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action.'?';
$this->method=$method;
$this->target=$target;
}另外,觉得你的那个圆形貌似有问题,都没传参数。
纯肉眼再看,瞎猜,有问题再说~
$str.="<input type=hidden name='act' value=".$this->request["action"].">";
$str.="<input type=hidden name='action' value=".$this->request["action"].">";
这两个是干什么用的啊?
class__Rect.php 不能是 class__rect.php 或其他action 用于控制表单加载
act 用于控制类加载
class__form.php:
<?php
//表格类,根据不同的选择,创建不同的表格,可选择的有矩形,圆形和三角形。
class Form{
//声明表格的成员属性
private $formName;//表单的名称
private $request;//表单提供的变量,数组类型
private $action;//数据提交到的页面
private $method;//提交的方式
private $target;//新页面打开的方式,默认为_self
function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="<table align=center border=5 width=800><caption><h2>计算".$this->formName."的面积和周长</h2><caption>";
$str.="<form action=".$this->action." method=".$this->method." target=".$this->target.">";
switch($this->request["action"]){
case 1:
$str.="<tr><th>矩形长度:</th><td>";
$str.="<input type=text name='length' value=".$this->request["length"].">";
$str.="</td></tr><tr><th>矩形宽度:</th><td>";
$str.="<input type=text name='width' value=".$this->request["width"].">";
$str.="</td></tr>";
break;
case 2:
$str.="<tr><th>三角形第一条边长:</th><td>";
$str.="<input type=text name='length1' value=".$this->request["length1"].">";
$str.="</td></tr><tr><th>三角形第二条边长:</th><td>";
$str.="<input type=text name='length2' value=".$this->request["length2"].">";
$str.="</td></tr><tr><th>三角形第三条边长:</th><td>";
$str.="<input type=text name='length3' value=".$this->request["length3"].">";
$str.="</td></tr>";
break;
case 3:
$str.="<tr><th>圆形半径:</th><td>";
$str.="<input type=text name='radius' value=".$this->request["radius"].">";
$str.="</td></tr>";
break;
}
$str.="<tr><td align=center colspan=2><input type='submit' value='计算'></td></tr>";
$str.="<input type=hidden name='act' value=".$this->request["action"].">";
$str.="<input type=hidden name='action' value=".$this->request["action"].">";
$str.="</form></table>";
return $str;
}
}
?>
index.php:
<html>
<head>
<title>
计算图形的面积和周长
</title>
</head>
<body>
<?php
function __autoload($className){//包含到类时,自动加载到本页
include ("class__".ucfirst($className).".php");//自动加载相应的类所在的文件
}
?>
<center>
<a href="index.php?action=1">矩形</a>||
<a href="index.php?action=2">三角形</a>||
<a href="index.php?action=3">圆形</a><hr />
</center>
<?php
switch($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
}if(isset($_REQUEST["act"])){
switch ($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo "面积:".$shape->area()."<br />";
echo "周长:".$shape->perimeter()."<br />";
}
?>
</body>
</html>
其他的代码没有变,到底问题出在哪啊??
class Form{
private $formName;
private $request;
private $action;
private $method;
private $target;function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="<table align=center border=5 width=800>";
$str.="<caption><h2>".$this->formName."</h2><caption>";
$str.="<form action='$this->action' method='$this->method' target=$this->target";switch ($this->request["action"]){
case 1:
$str.="<tr><th>矩形长度:</th><td>";
$str.="<input type='text' name='length' value=".$this->request["length"]."></td></tr>";
$str.="<tr><th>矩形宽度:</th><td>";
$str.="<input type='text' name='width' value=".$this->request["width"]."></td></tr>";
break;
case 2:
$str.="<tr><th>三角形边长1:</th><td>";
$str.="<input type=text name='length1' value=".$this->request["length1"]."></td></tr>";
$str.="<tr><th>三角形边长2:</th><td>";
$str.="<input type=text name='length2' value=".$this->request["length2"]."></td></tr>";
$str.="<tr><th>三角形边长3:</th><td>";
$str.="<input type=text name='length3' value=".$this->request["length3"]."></td></tr>";
break;
case 3:
$str.="<tr><th>圆形半径:</th><td>";
$str.="<input type=text name='radius' value=".$this->request["radius"]."></td></tr>";
break;
}
$str.="<tr><td align=center colspan=2><input type=submit value='计算'></td></tr>";
$str.="<input type=hidden name='act' value=".$this->request["action"].">";
$str.="<input type=hidden name='action' value=".$this->request["action"].">";
$str.="</form></table>";return $str;
}
}
?>index.php<html>
<head>
<title>图形计算器</title>
</head>
<body>
<?php
function __autoload($className){
include ('class__'.ucfirst($className).'.php');
}
?>
<center>
<h2>图形周长和面积计算器
</h2><hr>
<a href="index.php?action=1">矩形</a>||
<a href="index.php?action=2">三角形</a>||
<a href="index.php?action=3">圆形</a><br />
</center>
<?php
switch ($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
default:
echo "请选择一个图形<br />";
}if(isset($_REQUEST["act"])){
switch($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}echo "面积为:".$shape->area()."<br>";
echo "周长为:".$shape->perimeter()."<br>";
}
?>
</body>
</html>