標籤:style blog http os io 檔案 for ar 2014
曾經看細說PHP的時候就想做這個,可是一直沒什麼時間,這次總算忙裡偷閒搞了代碼量比較多的project。
首先,文檔結構,都在一個檔案夾下就好了,我的就例如以下。
一開始,進入index.php檔案。
<html><head><title>圖形計算機(物件導向)</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8"></head><body><center><h1>圖形(面積 周長)計算機)</h1><a href="index.php?action=rect">矩形</a><!-- action 是動作提交 -->|| <a href="index.php?action=triangle">三角形</a>||<a href="index.php?action=circle">圓形</a><hr></center> <?phperror_reporting ( E_ALL & ~ E_NOTICE );//PHP遇到不認識的類就會調用該方法自己主動載入function __autoload($className) {include strtolower ( $className ) . ".class.php";}/* *由於遇到Form類不認識,所以自己主動載入form.class.php * */echo new Form ('index.php');if (isset ( $_POST ["sub"] )) {echo new Result ();}?> </body></html>
做了這麼幾件事
1.能夠通過$GET_[“action”]方法和$_REQUEST ["action"]方法得到傳入的參數是rect, triangle還是circle。
2.通過echo new Form("index.php")和echo new Result()方法載入了form.class.php和result.class.php這兩個類
3.通過echo調用了這兩個類的__toString方法
接下來程式載入form.class.php這個檔案
<?phpclass Form {private $action;private $shape;function __construct($action = "") {$this->action = $action;/* var_dump($this->action); */$this->shape = isset ( $_REQUEST ["action"] ) ? $_REQUEST ["action"] : "rect";/* var_dump($this->shape); */}/* __toString() 方法用於一個類被當成字串時應如何回應。比如 echo $obj; 應該顯示些什麼。此方法必須返回一個字串 * * 在此輸出一個表單 * * */function __toString() {$form = '<form action="' . $this->action . '" method="post">';switch ($this->shape) {case "rect" :$form .= $this->getRect ();break;case "triangle" :$form .= $this->getTriangle ();break;case "circle" :$form .= $this->getcircle ();break;default :$form .= '請選擇一個形狀';}$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="test" name="height" value="' . $_POST ['height'] . '"><br>';$input .= '<input type="hidden" name="action" value="rect">';return $input;}private function getTriangle() {$input = '<b>請輸入|三角形|的三邊:</b><p>';$input = '<b>請輸入|三角形|的三邊:</b><p>';$input .= '第一邊:<input type="text" name="side1" value="' . $_POST ['side1'] . '"><br>';$input .= '第二邊:<input type="test" name="side2" value="' . $_POST ['side2'] . '"><br>';$input .= '第三邊:<input type="test" 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;}}?>
這個php做了另外一件事,就是依據$_REQUEST ["action"]或者GET_["action"]載入了不同類型形狀的表單。
然後,一旦你按下計算button,接下來就轉到了載入result.class.php這個檔案
<?php class Result{ private $shape; /* * 依據form.class.php裡傳過來的$post['action']方法接受參數 * */ 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;//沒有break會導致default的運行 default: $this->shape=false; } } 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'沒有這個形狀'; }}} ?>
這個檔案做了一件事,就是分流,依據$_POST["action"]傳過來的值看,運行那一個類檔案
下一篇講各個類檔案含義。
php:兄弟連之物件導向版圖形計算機1