值不是从HTML表单传递到PHP类

时间:2016-08-21 13:25:27

标签: php oop

  

已编辑:已将检查停止添加到代码中。

保持OOP设计,我做错了什么? 三角形边没有正确地进入类中。

我已经在PHP文件中使用参数测试了对象,并且它的工作非常出色。因此,似乎唯一的罪魁祸首就是我如何在课程中传递参数。

这是我的 shape.php

    <?php
abstract class Shape{
    abstract protected function getArea();
    abstract protected function getPerimeter();
}

 class Triangle extends Shape{
    private $_sides = array();
    private $_perimeter = null;
     public $status = 0;

     function __construct($s0=0, $s1=0, $s2=0){
         $this->_sides[] = $s0;
         $this->_sides[] = $s1;
         $this->_sides[] = $s2;
         echo 'constructed<hr>';

         //calculate perimeter:
         $this->_perimeter = array_sum($this->_sides);
         echo 'calculated perimeter<hr>';
         $this->checkForm();
     }
     public function checkForm(){
         if (!empty($_POST['submit'])){
             $checkIt = $this->status = 1;
/*             echo 'status is <hr>'.$checkIt;*/

         }
         return $this->status;
     }
     public function proceed(){
         if ($this->status == 1){
             echo 'proceed func started<hr>';
             $this->_sides[0] = $_POST['s0'];
             $this->_sides[1] = $_POST['s1'];
             $this->_sides[2] = $_POST['s2'];

             echo 'Side 1: '.$this->_sides[0] = $_POST['s0'].'<hr>';
             echo 'Side 2: '.$this->_sides[1] = $_POST['s1'].'<hr>';
             echo 'Side 3: '.$this->_sides[2] = $_POST['s2'].'<hr>';


         }else{
             echo 'This didn\'t work as planned... :(';
         }
     }

    public function getArea(){

        return (sqrt(
            ($this->_perimeter/2) *
            (($this->_perimeter/2)- $this->_sides[0]) *
            (($this->_perimeter/2)- $this->_sides[1]) *
            (($this->_perimeter/2)- $this->_sides[2])
        ));
    }
    public function getPerimeter(){
        return $this->_perimeter;
    }
    }

/*$check = new Triangle(2, 2, 2);*/
$check = new Triangle();
echo $check->proceed();
echo 'status is: '.$check->status.'<hr>';

echo 'The '.get_parent_class($check).' is a '.get_class($check).'. It\'s Perimeter is: '.$check->getPerimeter().'M.<br>';
echo 'The '.get_parent_class($check).' also has an area of: '.$check->getArea().'M.<br>';

这是我的 index.php 文件:

<div class="row">
<div class="boxy">
<form method="post" action="Shape.php">
<label for="s0">Side 1</label>
<input name="s0" type="number" placeholder="Side 0"><br>
<label for="s1">Side 2</label>
<input name="s1" type="number" placeholder="Side 1"><br>
<label for="s2">Side 3</label>
<input name="s2" type="number" placeholder="Side 2"><br>
    <input type="submit" name="submit" value="Run It.">
</form>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题,例如:

  • 您正在尝试在构造函数中设置三角形属性,但您没有传递任何值。见这个陈述,

    $check = new Triangle();
    
  • 您未使用checkForm()proceed()方法。

虽然有几种解决方案可以解决这个问题,但是可以这样:

  • 将构造函数方法保持为空,如下所示:

    function __construct(){}
    
  • 并按以下方式更改您的proceed()getArea()getPerimeter()方法,

    private function proceed(){
        if ($this->status == 1){
            $this->_sides[0] = isset($_POST['s0']) ? $_POST['s0'] : 0;
            $this->_sides[1] = isset($_POST['s1']) ? $_POST['s1'] : 0;
            $this->_sides[2] = isset($_POST['s2']) ? $_POST['s2'] : 0;
        }else{
            echo 'This didn\'t work as planned... :(';
        }
    }
    
    public function getArea(){
        if($this->checkForm()){
            $this->proceed();
            return (sqrt(
                ($this->_perimeter/2) *
                (($this->_perimeter/2)- $this->_sides[0]) *
                (($this->_perimeter/2)- $this->_sides[1]) *
                (($this->_perimeter/2)- $this->_sides[2])
            ));
        }else{
            echo "Something went wrong. ";
        }
    }
    public function getPerimeter(){
        if($this->checkForm()){
            $this->proceed();
            return $this->_perimeter = array_sum($this->_sides);
        }else{
            echo "Something went wrong. ";
        }
    }
    

答案 1 :(得分:0)

好吧,我实际上发现了错误,我忘了返回proceed()结果。

这里修改完全正常:

abstract class Shape{
    abstract protected function getArea();
    abstract protected function getPerimeter();
}

 class Triangle extends Shape{
    private $_sides = array();
    private $_perimeter = null;
     public $status = 0;

     function __construct($s0=0, $s1=0, $s2=0){
         $this->_sides[] = $s0;
         $this->_sides[] = $s1;
         $this->_sides[] = $s2;
         echo 'constructed<hr>';

         //calculate perimeter:
         $this->_perimeter = array_sum($this->_sides);
         echo 'calculated perimeter<hr>';
         $this->checkForm();
     }
     public function checkForm(){
         if (!empty($_POST['submit'])){
             $checkIt = $this->status = 1;
/*             echo 'status is <hr>'.$checkIt;*/

         }
         return $this->status;
     }
     public function proceed(){
         if ($this->status == 1){
             echo 'proceed func started<hr>';
             $this->_sides[0] = $_POST['s0'];
             $this->_sides[1] = $_POST['s1'];
             $this->_sides[2] = $_POST['s2'];

             echo 'Side 1: '.$this->_sides[0] = $_POST['s0'].'<hr>';
             echo 'Side 2: '.$this->_sides[1] = $_POST['s1'].'<hr>';
             echo 'Side 3: '.$this->_sides[2] = $_POST['s2'].'<hr>';




         }else{
             echo 'This didn\'t work as planned... :(';
         }
         return $this->_sides;
     }

    public function getArea(){

        return (sqrt(
            ($this->_perimeter/2) *
            (($this->_perimeter/2)- $this->_sides[0]) *
            (($this->_perimeter/2)- $this->_sides[1]) *
            (($this->_perimeter/2)- $this->_sides[2])
        ));
    }
    public function getPerimeter(){
        return $this->_perimeter;
    }

    }

$check = new Triangle($_POST['s0'], $_POST['s1'], $_POST['s2']);
/*$check = new Triangle();*/
echo 'status is: '.$check->status.'<hr>';

echo 'The '.get_parent_class($check).' is a '.get_class($check).'. It\'s Perimeter is: '.$check->getPerimeter().'M.<br>';
echo 'The '.get_parent_class($check).' also has an area of: '.$check->getArea().'M.<br>';