Triangle calculations
The one-time recording of multiple class files mentioned in the previous section can actually be achieved using the magic release __autoload() provided by PHP
The code is as follows:
<?php function __autoload($classname) { //魔術(shù)方法 自動(dòng)加載類 require "./$classname.class.php"; //將類名轉(zhuǎn)化成小寫(xiě) }
This method will be automatically called as long as the corresponding class is instantiated. The system internal code It will automatically find the name of the class file and assign it to $classname,
The advantage of this is that it can delay loading, and this method will only be loaded when the corresponding class is instantiated. , avoiding the introduction of many class files at once, and may not use the
triangle calculation method
New Triangle.class.php file,
For a triangle, please note that the three sides must satisfy that the sum of the two sides is greater than the third side, and cannot be letters or less than 0. The calculation methods of area and perimeter are different. , other structures are basically similar to rectangles
The specific code is as follows:
<?php class Triangle extends Shape { private $bian1; private $bian2; private $bian3; function __construct($arr = array()) { if (!empty($arr)) { $this->bian1 = $arr['bian1']; $this->bian2 = $arr['bian2']; $this->bian3 = $arr['bian3']; } $this->name = "三角形"; $this->error = ''; } function area() { $p = ($this->bian1 + $this->bian2 + $this->bian3) / 2; // p(p-a)(p-b)(p-c) return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3)); } function zhou() { return $this->bian1+$this->bian2+$this->bian3; } function view($arr) { $form=''; $form .= "<form action='index.php?action=triangle' method='post'>"; $form .= "請(qǐng)輸入".$arr['name']."的第一條邊:<input type='text' name='bian1' value='".$_POST['bian1']."'/><br>"; $form .= "<br>"; $form .= "請(qǐng)輸入".$arr['name']."的第二條邊:<input type='text' name='bian2' value='".$_POST['bian2']."'/><br>"; $form .= "<br>"; $form .= "請(qǐng)輸入".$arr['name']."的第三條邊:<input type='text' name='bian3' value='".$_POST['bian3']."'/><br>"; $form .= "<br>"; $form .= "<input type='submit' name='sub' value='提交'/> "; $form .= "<input type='reset' name='ret' value='重置'/>"; $form .= "</form>"; echo $form; } function yan($arr) { $bz = true; if ($arr['bian1']< 0) { $this->error .= "第一條邊小于0;"; $bz = false; } else { if (!is_numeric($arr['bian1'])) { $this->error .= "第一條邊不是數(shù)字;"; $bz = false; } } if ($arr['bian2']< 0) { $this->error .= "第二條邊小0;"; $bz = false; } else { if (!is_numeric($arr['bian2'])) { $this->error .= "第二條邊不是數(shù)字;"; $bz = false; } } if ($arr['bian2']< 0) { $this->error .= "第三條邊小于0;"; $bz = false; } else { if (!is_numeric($arr['bian2'])) { $this->error .= "第三條邊不是數(shù)字;"; $bz = false; } } if (($this->bian1+$this->bian2) < $this->bian3 ||($this->bian1+$this->bian3) < $this->bian2 ||($this->bian2+$this->bian3) < $this->bian1) { $this->error .= "三條邊不能構(gòu)成三角形"; $bz = false; } return $bz; } } ?>
Running result display: