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) { //魔术方法 自动加载类
    require "./$classname.class.php"; //将类名转化成小写
}


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 .= "请输入".$arr['name']."的第一条边:<input type='text' name='bian1' value='".$_POST['bian1']."'/><br>";
        $form .= "<br>";
        $form .= "请输入".$arr['name']."的第二条边:<input type='text' name='bian2' value='".$_POST['bian2']."'/><br>";
        $form .= "<br>";
        $form .= "请输入".$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 .= "第一条边不是数字;";
                $bz = false;
            }
        }
        if ($arr['bian2']< 0) {
            $this->error .= "第二条边小0;";
            $bz = false;
        } else {
            if (!is_numeric($arr['bian2'])) {
                $this->error .= "第二条边不是数字;";
                $bz = false;
            }
        }
        if ($arr['bian2']< 0) {
            $this->error .= "第三条边小于0;";
            $bz = false;
        } else {
            if (!is_numeric($arr['bian2'])) {
                $this->error .= "第三条边不是数字;";
                $bz = false;
            }
        }
        if (($this->bian1+$this->bian2) < $this->bian3 ||($this->bian1+$this->bian3) < $this->bian2 ||($this->bian2+$this->bian3) < $this->bian1) {
            $this->error .= "三条边不能构成三角形";
            $bz = false;
        }
        return $bz;
    }
}
?>

Running result display:
gif5新文件 (30).gif

Continuing Learning
||
<?php echo "三角形的周长和面积的计算";
submitReset Code