简体   繁体   English

值未从HTML表单传递到PHP类

[英]Values are not passing from HTML form into a PHP class

EDITED: Added checking stops into code. 编辑:将检查停止添加到代码中。

keeping OOP design, what am I doing wrong? 保持OOP设计,我在做什么错? The triangle sides are not passing into the classes properly. 三角形边未正确传递到类中。

I already tested the object with parameters inside the PHP file and it's working excellent. 我已经使用PHP文件中的参数测试了对象,并且效果很好。 So it seems that the only culprit here is how I'm passing the params inside the classes. 因此,似乎唯一的罪魁祸首是我如何在类中传递参数。

Here is my shape.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>';

And this is my index.php file: 这是我的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>

There are few issues with your code, such as: 您的代码几乎没有问题,例如:

  • You're trying to set up triangle properties in the constructor but you're not passing any values. 您试图在构造函数中设置三角形属性,但未传递任何值。 See this statement, 看到这句话,

     $check = new Triangle(); 
  • You didn't make use of checkForm() and proceed() method. 您没有使用checkForm()proceed()方法。

Though there are several solutions to this problem, one would be like this: 尽管有多种解决方案可以解决此问题,但是这样的解决方案是:

  • Keep your constructor method empty, like this: 保持构造函数方法为空,如下所示:

     function __construct(){} 
  • And change your proceed() , getArea() and getPerimeter() methods in the following way, 然后按照以下方式更改您的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. "; } } 

Well, I actually found the error, I forgot to return the proceed() results. 好吧,我实际上发现了错误,我忘了返回proced()结果。

Here it is amended and fully working: 在这里进行了修改并可以正常使用:

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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM