效果:php
index.phphtml
<!DOCTYPE html> <html> <head> <title>圖形計算(使用面向對象技術開發)</title> <meta http-equiv="content" content="text/html" charset="utf-8" /> </head> <body> <center> <!--居中--> <h1>圖形(周長&面積)計算器</h1> <!--計算圖形的連接--> <a href="index.php?action=rect">矩形</a>|| <a href="index.php?action=triangle">三角形</a>|| <a href="index.php?action=circle">圓形</a> <hr> <!--建立一條水平分隔線--> </center> <?php //錯誤報告處理 //error_reporting(E_ALL & ~E_NOTUCE); //自動加載須要的類文件 function __autoload($className){ //strtolower()函數,把類名轉化爲小寫 include strtolower($className).".class.php"; } echo new Form(); if(isset($_POST["sub"])){ echo new Result(); } ?> </body> </html>
form.class.phpide
<?php class Form{ private $action; private $shape; //在PHP5中的構造方法 function __construct($action=""){ $this->action=$action; $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect"; } //在直接輸出對象引用的時候自動調用 function __toString(){ $form='<form action="'.$this->action.'" method="post">'; //echo $this->shape; switch($this->shape){ case "rect": //要加到表單裏面,要返回字符串 $form.=$this->getRect(); break; case "triangle": $form.=$this->getTriangle(); break; case "circle": $form.=$this->getCircle(); break; default: $form.='請選擇一個形狀<br>'; } $form.='<input type="submit" name="sub" value="計算">'; $form.='</form>'; return $form; } //獲得矩形的方法 private function getRect(){ $input='<b>請輸入矩形的寬度和高度:</b><p>'; $input.='寬度:<input type="text" name="width" value="'.$_POST["width"].'"><br>'; $input.='高度:<input type="text" name="height" value="'.$_POST["height"].'"><br>'; $input.='<input type="hidden" name="action" value="rect">'; return $input; } //獲得三角形的方法 private function getTriangle(){ $input='<b>請輸入三角形的三條邊:</b><p>'; $input.='第一條邊:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>'; $input.='第二條邊:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>'; $input.='第三條邊:<input type="text" name="side3" value="'.$_POST["side3"].'"><br>'; $input.='<input type="hidden" name="action" value="triangle">'; return $input; } //獲得圓形的方法 private function getCircle(){ $input='<b>請輸入圓形的半徑:</b><p>'; $input.='半徑:<input type="text" name="radius" value="'.$POST["radius"].'"><br>'; $input.='<input type="hidden" name="action" value="circle">'; return $input; } } ?>
shape.class.php函數
<?php abstract class Shape{ public $shapeName; abstract function area(); abstract function perimeter(); //驗證 protected function validate($value,$message="形狀"){ if($value==""||!is_numeric($value)||$value<0){ echo '<font color="red">'.$message.'必須爲非負值的數字,而且不能爲空!</font><br>'; return false; }else{ return true; } } } ?>
result.class.phppost
<?php class Result{ private $shape; function __construct(){ switch ($_POST['action']) { case 'rect': $this->shape=new Rect(); break; case 'triangle': $this->shape=new Triangle(); break; case 'circle': $this->shape=new Circle(); break; default: $this->shape=false; break; } } //在直接輸出對象引用的時候自動調用 function __toString(){ if($this->shape){ $result=$this->shape->shapeName."的周長:".$this->shape->perimeter().'<br>'; $result.=$this->shape->shapeName."的面積:".$this->shape->area().'<br>'; return $result; }else{ return "沒有這個形狀<br>"; } } } ?>
Rect.class.phpui
<?php class Rect extends Shape{ private $width=0; private $height=0; function __construct(){ $this->shapeName="矩形"; if($this->validate($_POST["width"],'矩形的寬') & $this->validate($_POST["height"],"矩形的長")){ $this->width=$_POST["width"]; $this->height=$_POST["height"]; }else{ exit; } } //面積 function area(){ return $this->width*$this->height; } //周長 function perimeter(){ return 2*($this->width+$this->height); } } ?>
Triangle.class.phpthis
<?php class Triangle extends Shape{ private $side1=0; private $side2=0; private $side3=0; function __construct(){ $this->shapeName="三角形"; if($this->validate($_POST["side1"],'三角形的第一條邊') & $this->validate($_POST["side2"],"三角形的第二條邊") & $this->validate($_POST["side3"],"三角形的第三條邊")){ $this->side1=$_POST["side1"]; $this->side2=$_POST["side2"]; $this->side3=$_POST["side3"]; if(!$this->validateSum()){ echo '<font color="red">三角形的兩邊之和必須大於第三邊!</font><br>'; exit; } }else{ exit; } } //海倫公式 function area(){ $s=($this->side1+$this->side2+$this->side3)/2; return sqrt($s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3)); } function perimeter(){ return $this->side1+$this->side2+$this->side3; } //驗證兩邊之和大於第三邊 private function validateSum(){ $condition1=($this->side1 + $this->side2)> $this->side3; $condition2=($this->side1 + $this->side3)>$this->side2; $condition3=($this->side2 + $this->side3)>$this->side1; if($condition1 & $condition2 & $condition3){ return true; }else{ return false; } } } ?>
Circle.class.phpspa
<?php class Circle extends Shape{ private $radius=0; function __construct(){ $this->shapeName="圓形"; if($this->validate($_POST["radius"],'圓的半徑')){ $this->radius=$_POST["radius"]; }else{ exit; } } function area(){ return pi()*$this->radius*$this->radius; } function perimeter(){ return 2*pi()*$this->radius; } } ?>