Rectangle calculation

Considering that there are many kinds of graphics, we can first build a graphics abstract class to provide properties and methods common to all graphics,

so that other graphics can directly inherit it , reducing code redundancy and conforming to object-oriented thinking

1, create a new Shap.class.php file

The name of the graphic $name, the error message $error, the perimeter area() and the area zhou(), and there is a verification yan() after each calculation

<?php
abstract class Shape {
    private $name;
    private $error;
    abstract function area();
    abstract function zhou();
    abstract function view($arr);
    abstract function yan($arr);
}
?>

2, Create a new Rect.class.php file

After creating a new rectangle class, inherit the graphics class and implement the corresponding methods. In addition to the inherited properties and methods, each graphics has its own unique Properties and methods, for example, a rectangle has length and width, a sphere has radius, etc.

Define the properties of the rectangle $width and $height

Use the constructor to instantiate it automatically Assign values ​​to name and error

Calculate the area and perimeter respectively

After clicking the rectangle, you need to display the input box. At this time, you only need to define a method view() to print out the input box. You can

Add an a tag to the rectangular button

<a href='index.php?action=rect'>rectangle</a> |

After clicking, perform a get request and display the input box printed in the view. The code is as follows:

<?php
if (!empty($_GET['action'])) {
    $shape = new Rect();
    $shape->view();
    }
<?php
class Rect extends Shape {
    private $width;
    private $height;
    function __construct() {
        $this->name = "矩形";
        $this->error = '';
    }
    function area() {
        return $this->width * $this->height;
    }
    function zhou() {
        return ($this->width+$this->height) * 2;
    }
    function view($arr) {
        $form='';
        $form .= "<form action='index.php?action=rect' method='post'>";
        $form .= "请输入".$arr['name']."的宽度:<input type='text' name='width' value='".$_POST['width']."'/><br>";
        $form .= "<br>";
        $form .= "请输入".$arr['name']."的长度:<input type='text' name='height' value='".$_POST['height']."'/><br>";
        $form .= "<br>";
        $form .= "<input type='submit' name='sub' value='提交'/>    ";
        $form .= "<input type='reset' name='ret' value='重置'/>";
        $form .= "</form>";
        echo $form;
    }
}
?>

3, printing of verification information

New method added in rectangle class:

<?php
function yan($arr) {
    $bz = true;

    if ($arr['width']< 0) {
        $this->error .= "宽度小于0;";
        $bz = false;
    } else {
        if (!is_numeric($arr['width'])) {
            $this->error .= "宽不是数字;";
            $bz = false;
        }
    }
    if ($arr['height']< 0) {
        $this->error .= "宽度小于0;";
        $bz = false;
    } else {
        if (!is_numeric($arr['height'])) {
            $this->error .= "高不是数字;";
            $bz = false;
        }
    }
    return $bz;
}

Print verification information, index.php code:

if (!$shape->yan($_POST)) {
    echo "<b>错误:$shape->error</b>";
}
echo "</div>";

If the verification information is correct, print the correct perimeter and area:

<?php
echo "<div id='footer'>";
if ($shape->yan($_POST)) {
    echo "<b>".$shape->name."的周长".$shape->zhou()."</b>"."<br>";
    echo "<br>";
    echo "<b>".$shape->name."的面积".$shape->area()."</b>"."<br>";
}else {
    echo "<b>错误:$shape->error</b>";
}
echo "</div>";

Running results:

gif5新文件 (29).gif

Thinking: You need to introduce the Rect.class.php file in index.php, require 'Rect.class. php';

It also needs to be imported when calculating other graphics. Is there any way to import all the class files at once? (Introduced in the next section)

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