這個例子可能並不實用,但基本概括了面向對象的三個特征:繼承性,封裝性,多態性。本例的主要功能有:
效果如下:

思路:
需要改進的地方:
index.php代碼如下:
1 <html>
2 <head>
3 <meta http-equiv="charset" content="utf-8">
4 </head>
5 <body>
6 <div id="center">
7 <h1>圖形周長面積計算器</h1>
8 <!--點擊鏈接的時候使用GET傳遞圖形的形狀屬性給index.php,也就是頁面本身-->
9 <a href="index.php?shape=rect">矩形</a>
10 <a href="index.php?shape=triangle">三角形</a>
11 <a href="index.php?shape=circle">圓形</a>
12 </div>
13 <div id="inputForm">
14 <?php
15 /*自動加載類*/
16 function __autoload($className){
17 include (strtolower($className).'.class.php');
18 }
19
20 /*
21 1.先new一個Form對象,發現沒有form類的定義,把類名Form傳遞到自動加載類的函數參數進行類的自動加載。
22 2.echo一個對象的引用,會調用該對象的__toString方法返回一個字符串,echo輸出的就是對象返回的字符串,
23 這裡輸出一個表單等待用戶的輸入。
24 */
25 echo new Form("index.php");
26
27 /*如果用戶點擊了提交按鈕,自動加載result類,輸出結果*/
28 if(isset($_POST["sub"])){
29 echo new result();
30 }
31 ?>
32 </div>
33 </body>
34 </html>
form.class.php代碼如下:
1 <?php
2 /*
3 project:面向對象版圖形計算器
4 file:form.class.php
5 description:對不同的圖形輸出不同的表單
6 */
7 class form{
8 private $formAction=NULL; //保存響應表單的文件
9 private $shape=NULL; //保存圖形的形狀
10
11 /*
12 @param string $action 對象初始化傳入的參數,代表響應的頁面的是哪一個文件
13 */
14 function __construct($action = ""){
15 $this->formAction = $action; //把傳入的參數保存到$formAction中;
16 $this->shape = isset($_GET["shape"]) ? $_GET["shape"]:"rect"; //從表單傳遞的變量中獲取圖形類別,如沒有傳遞,默認為矩形
17 }
18 function __toString(){
19 $form = '<form action="'.$this->formAction.'?shape='.$this->shape.'" method="post">';
20 //下面兩行使用變量函數調用對應圖形的私有函數,返回input部分表單的字符串
21 $shape = 'get'.ucfirst($this->shape);
22 $form .= $this->$shape();
23
24 $form .= '</br><input type="submit" name="sub" value="計算"/></br>';
25 $form .= '</form>';
26
27 return $form;
28 }
29 //私有方法,返回矩形表單input部分的字符串;
30 private function getRect(){
31 //在表單提交後輸入的內容繼續顯示,防止其消失
32 $formheight=isset($_POST['height']) ? $_POST['height'] : NULL;
33 $formwidth=isset($_POST['width']) ? $_POST['width'] : NULL;
34 $input = '<p>請輸入矩形的長和寬</p>';
35 $input .= '矩形的高度:<input type="text" name="height" value="'.$formheight.'"/><br></br>';
36 $input .= '矩形的寬度:<input type="text" name="width" value="'.$formwidth.'"/></br>';
37 return $input;
38 }
39 //返回三角形輸入表單input部分的字符串
40 private function getTriangle(){
41 //在表單提交後繼續顯示出來,防止其消失
42 $formside1=isset($_POST['side1']) ? $_POST['side1'] : NULL;
43 $formside2=isset($_POST['side2']) ? $_POST['side2'] : NULL;
44 $formside3=isset($_POST['side3']) ? $_POST['side3'] : NULL;
45 $input = '<p>請輸入三角形的三邊</p>';
46 $input .= '邊長1:<input type="text" name="side1" value="'.$formside1.'" /></br></br>';
47 $input .= '邊長2:<input type="text" name="side2" value="'.$formside2.'"/></br></br>';
48 $input .= '邊長3:<input type="text" name="side3" value="'.$formside3.'"/></br>';
49 return $input;
50 }
51 //返回圓形表單input部分的字符串
52 private function getCircle(){
53 $formradius=isset($_POST['radius']) ? $_POST['radius'] : NULL; //在輸入表單提交後內容繼續顯示出來,防止其消失
54 $input = '<p>請輸入半徑</p>';
55 $input .= '半徑:<input type="text" name="radius" value="'.$formradius.'"/></br>';
56 return $input;
57 }
58 }
59
result.class.php代碼如下:
1 <?php
2 class result{
3 private $shape = NULL;
4
5 //使用GET傳遞的變量,實例化一個相應的對象,返回一個對象的引用;
6 function __construct(){
7 $this->shape = new $_GET["shape"]();
8 }
9 //調用對象的屬性和方法,返回周長和面積
10 function __toString(){
11 $result = $this->shape->shapeName.'的周長為'.$this->shape->perimeter().'</br>';
12 $result .= $this->shape->shapeName.'的面積為'.$this->shape->area().'</br>';
13 return $result;
14 }
15 }
抽象類shape.class.php代碼如下:
1 <?php
2 /*
3 project:面向對象版圖形計算器
4 file:shape.class.php
5 description:抽象類,定義兩個抽象方法area()和perimeter(),以及定義方法validate對輸入的值進行驗證
6 */
7 abstract class shape{
8 public $shapeName; //形狀名稱;
9 abstract function area(); //抽象類area(),讓子類去實現,體現多態性
10 abstract function perimeter(); //抽象類perimeter();
11
12 /*
13 @param mixed $value 接受表單輸入值
14 @param string $message 提示消息前綴
15 @param boolean 返回值,成功為TRUE,失敗為FALSE
16 */
17 protected function validate($value,$message = "輸入的值"){
18 if($value < 0 || $value == NULL || !is_numeric($value)){
19 $message = $this->shapeName.$message;
20 echo '<font color="red">'.$message.'必須為正數</font><br>';
21 return FALSE;
22 }
23 else
24 return TRUE;
25 }
26 }
子類triangle.class.php代碼如下:
1 <?php
2 /**
3 project:面向對象版圖形計算器
4 file:triangle.class.php
5 description:繼承抽象類shape,計算並返回三角形的周長和面積
6 */
7 class triangle extends shape{
8 private $side1 = 0; //邊長1;
9 private $side2 = 0; //邊長2;
10 private $side3 = 0; //邊長3;
11
12 /*
13 構造函數:對表單變量進行合理性驗證,通過則初始化三個邊長
14 */
15 function __construct(){
16 $this->shapeName = "三角形"; //命名圖形
17
18 //使用父類的方法validate檢查輸入的是否為正數
19 if($this->validate($_POST["side1"],"邊長1") & $this->validate($_POST["side2"],"邊長2") & $this->validate($_POST["side3"],"邊長3")){
20
21 //使用私有方法驗證兩邊和是否大於第三邊
22 if($this->validatesum($_POST["side1"],$_POST["side2"],$_POST["side3"])){
23 $this->side1 = $_POST["side1"]; //若通過驗證初始化三邊;
24 $this->side2 = $_POST["side2"];
25 $this->side3 = $_POST["side3"];
26 }
27 else{
28 echo '<font color="red">兩邊的和要大於第三邊</font>';
29 exit();
30 }
31 }
32 else{
33 exit();
34 }
35 }
36 /*使用海倫公式計算面積,並返回結果*/
37 function area(){
38 $s = ($_POST["side1"] + $_POST["side2"] + $_POST["side3"])/2;
39 return sqrt($s * ($s - $_POST["side1"]) * ($s - $_POST["side2"]) * ($s - $_POST["side3"]));
40 }
41 /*計算並返回周長*/
42 function perimeter(){
43 return $_POST["side1"] + $_POST["side2"] + $_POST["side3"];
44 }
45 /*計算三角形兩邊和是否大於第三邊,是返回TRUE,否返回FALSE*/
46 private function validatesum($side1,$side2,$side3){
47 if(($side1 + $side2) > $side3 && ($side1 + $side3) > $side2 && ($side2 + $side3) > $side1)
48 return TRUE;
49 else
50 return FALSE;
51 }
52 }
子類circle.class.php代碼如下:
1 <?php
2 /*
3 project:面向對象的圖形計算器
4 file:circle.class.php
5 description:接收表單值,返回周長和面積
6 */
7 class circle extends shape{
8 private $radius; //圓的半徑
9
10 //初始化圓的名稱,檢查輸入合法性並初始化半徑
11 function __construct(){
12 $this->shapeName = "圓形";
13 if($this->validate($_POST["radius"],"半徑"))
14 $this->radius = $_POST["radius"];
15 }
16 //返回圓的面積
17 function area(){
18 return 3.14 * $this->radius * $this->radius;
19 }
20 //返回圓的周長
21 function perimeter(){
22 return 3.14 * 2 * $this->radius;
23 }
24 }
子類rect.class.php代碼如下:
1 <?php
2 /*
3 project:面向對象的圖形計算器
4 file:rect.class.php
5 descrition:聲明一個矩形資料,實現形狀抽象類計算周長和面積的方法,返回矩形的周長和面積
6 */
7 class rect extends shape{
8 private $width; //矩形的寬度
9 private $height; //矩形的高度
10
11 //使用父類的validate方法驗證輸入的合法性,通過則初始化寬度和高度
12 function __construct(){
13 $this->shapeName = "矩形";
14 if($this->validate($_POST["width"],"寬度") && $this->validate($_POST["height"],"高度")){
15 $this->width = $_POST["width"];
16 $this->height = $_POST["height"];
17 }
18 }
19 //返回面積
20 function area(){
21 return $this->width * $this->height;
22 }
23 //返回周長
24 function perimeter(){
25 return 2 * ($this->width + $this->height);
26 }
27 }
聲明:
1.本文只適合實驗,不適合現實應用,若造成不良後果,本人概不負責。
2.本文為原創博客,可以在個人平台自由轉載,但需要注明出處,附上鏈接,否則視為盜用。嚴禁用於商業用途,如有需要,聯系本人支付稿費,授權後方能使用。