鍍金池/ 問答/PHP/ 關于php代碼的一些疑惑?

關于php代碼的一些疑惑?

 1 <html>
 2     <head>
 3         <title>圖形計算器開發(fā)</title>
 4         <meta http-equiv="Content-type" content="text/html;charset=utf-8">
 5     </head>
 6     
 7     <body>
 8         <center>
 9             <h1>圖形(周長&面積)計算器</h1>
10             
11             <a href="index.php?action=rect">矩形</a>
12             <a href="index.php?action=triangle">三角形</a>
13             <a href="index.php?action=circle">圓形</a>
14         </center>
15         <?php
16             error_reporting(E_ALL & ~E_NOTICE);
17             function __autoload($className){
18                 include strtolower($className).".class.php";
19             }
20             echo new Form("index.php");
21             if(isset($_POST["sub"])){
22                 echo new Result();
23             }
24         ?>
25     </body>
26 </html>
\\form.class.php
 1 <?php
 2     class Form{
 3         private $action;
 4         private $shape;
 5         function __construct($action=""){
 6             $this->action = $action;
 7             $this->shape = isset($_GET["action"])?$_GET["action"]:"rect";
 8         }
 9         
10         function __toString(){
11             $form='<form action="'.$this->action.'?action='.$this->shape.'" method="post">';
12             $shape="get".ucfirst($this->shape);
13             $form .=$this->$shape();
14             $form .='<br><input type="submit" name="sub" value="計算"><br>';
15             $form .='</form>';
16             return $form;
17         }
18         
19         private function getRect(){
20             $input = '<b>請輸入|矩形|的寬度和高度:</b><p>';
21             $input .= '寬度:<input type="text" name="width" value="'.$_POST["width"].'"><br>';
22             $input .= '高度:<input type="text" name="height" value="'.$_POST["height"].'"><br>';
23             return $input;
24         }
25         
26         private function getTriangle(){
27             $input = '<b>請輸入|三角形|的三條邊:</b><p>';
28             $input .= '第一條邊:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>';
29             $input .= '第二條邊:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>';
30             $input .= '第三條邊:<input type="text" name="side3" value="'.$_POST["side3"].'"><br>';
31             return $input;
32         }
33         
34         private function getCircle(){
35             $input = '<b>請輸入|圓形|的半徑:</b><p>';
36             $input .= '半徑:<input type="text" name="radius" value="'.$_POST["radius"].'"><br>';
37             return $input;
38         }
39     }
40 ?>
\\shape.class.php
 1 <?php
 2     abstract class Shape{
 3         public $shapeName;
 4         abstract function area();
 5         abstract function perimeter();
 6         protected function validate($value,$message = '輸入的值'){
 7             if($value=="" || !is_numeric($value) || $value<0){
 8                 $message=$this->shapeName.$message;
 9                 echo '<font color="red">'.$message.'必須為非負值的數(shù)字,并且不能為空</font><br>';
10                 return false;
11             }
12             else{
13                 return true;
14             }
15         }
16     }
17 ?>
\\result.class.php
 1 <?php
 2     class Result{
 3         private $shape = null;
 4         function __construct(){
 5             $this->shape = new $_GET['action']();
 6         }
 7         
 8         function __toString(){
 9             $result = $this->shape->shapeName.'的周長:'.round($this->shape->perimeter(),2).'<br>';
10             $result .= $this->shape->shapeName.'的面積:'.round($this->shape->area(),2).'<br>';
11             return $result;
12         }
13     }
14 ?>
rect.class.php
 1 <?php
 2     class Rect extends Shape{
 3         private $width = 0;
 4         private $height = 0;
 5         function __construct(){
 6             $this->shapeName = "矩形";
 7             if($this->validate($_POST["width"],"寬度") & $this->validate($_POST["height"],"高度")){
 8                 $this->width = $_POST["width"];
 9                 $this->height = $_POST["height"];
10             }
11         }
12         
13         function area(){
14             return $this->width*$this->height;
15         }
16         
17         function perimeter(){
18             return 2*($this->width+$this->height);
19         }
20     }
21 ?>
\\triangle.class.php
 1 <?php
 2     class Triangle extends Shape{
 3         private $side1 = 0;
 4         private $side2 = 0;
 5         private $side3 = 0;
 6         function __construct(){
 7             $this->shapeName = "三角形";
 8             if($this->validate($_POST["side1"],"第一條邊") & 
 9                 $this->validate($_POST["side2"],"第二條邊") & 
10                     $this->validate($_POST["side3"],"第三條邊")){
11                 if($this->validateSum($_POST["side1"],$_POST["side2"],$_POST["side3"])){
12                     $this->side1 = $_POST["side1"];
13                     $this->side2 = $_POST["side2"];
14                     $this->side3 = $_POST["side3"];
15                 }
16                 else{
17                     echo '<font color="red">三角形的兩邊之和要大于第三邊</font><br>';
18                 }    
19             }
20         }
21         
22         function area(){
23             $s = ($this->side1+$this->side2+$this->side3)/2;
24             return sqrt($s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3));
25         }
26         
27         function perimeter(){
28             return $this->side1+$this->side2+$this->side3;
29         }
30         
31         private function validateSum($s1,$s2,$s3){
32             if((($s1+$s2)>$s3) && (($s1+$s3)>$s2) && (($s2+$s3)>$s1)){
33                 return true;
34             }
35             else{
36                 return false;
37             }
38         }
39     }
40 ?>
\\circle.class.php
 1 <?php
 2     class Circle extends Shape{
 3         private $radius = 0;
 4         function __construct(){
 5             $this->shapeName = "圖形";
 6             if($this->validate($_POST['radius'],'半徑')){
 7                 $this->radius = $_POST['radius'];
 8             }
 9         }
10         
11         function area(){
12             return pi()*$this->radius*$this->radius;
13         }
14         
15         function perimeter(){
16             return 2*pi()*$this->radius;
17         }
18     }
19 ?>

第20行的代碼是什么意思?為什么傳入index.php這個參數(shù)。
還有form.class.php第7行的$_GET是哪來的,看了整個代碼沒看見哪里聲明用get傳遞數(shù)據(jù)。

回答
編輯回答
骨殘心

__autoload了解一下
超全局變量了解一下
手冊了解一下

2018年7月12日 00:34
編輯回答
膽怯

index.php 應該是個html頁面只不過用的.php后綴而已, 傳入index.php 這個參數(shù),然后 echo 是要輸出html內容的,并且echo 一個對象是會自動調用__toString這個方法的。

$_GET 變量是php內置的一個全局變量,存儲的是request中的get參數(shù)

2017年8月21日 07:33
編輯回答
怣痛

傳入index.php是因為要提交表單
$_GET是php自帶的,獲取get參數(shù)

2018年1月21日 03:22