2016-01-04 81 views
-1

我是一個初學者PHP面向對象編程。我想做一個簡單的計算器,我可以輸入兩個數字,並根據我的function計算它。我可以在OOP作爲參數只輸入兩個數字輸入。但是如何在<form>中輸入兩個數字並在<form>variable並顯示輸出?作爲初學者,請原諒我的錯誤。
謝謝先進。PHP OOP簡單的計算器製作

到目前爲止,我已經試過是:

的index.php:

<?php 
    require 'functions.php'; 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" > 
    <title>PHP OOP | Calculator</title> 
    <link href="style.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
<div class="wrapper"> 
    <div class="form_area"> 
     <form action="" method="POST"> 
      <table> 
       <tr> 
        <td>First Number :</td> 
        <td><input type="number" name="numOne" placeholder="Input Your First Number"></td> 
       </tr> 
       <tr> 
        <td>Second Number :</td> 
        <td><input type="number" name="numTwo" placeholder="Input Your Second Number"></td> 
       </tr> 
       <tr> 
        <td><input type="reset" name="reset" value="Reset"></td> 
        <td><input type="submit" name="submit" value="Calculate"></td> 
       </tr> 
      </table> 
     </form> 
    </div> 
    <div class="output_area"> 
     <?php 
      if(null !==($_POST['numOne'] && $_POST['numTwo'])){ 
       $resultsOne = new NumberCalculation(); 
       echo "Added Results = ".$resultsOne->add()."<br>"; 
      } 
     ?> 
     </div> 
    </div> 
    </body> 
</html> 

的functions.php:

<?php 

class NumberCalculation{ 
public $numOne; 
public $numTwo; 

public function __construct($firstNum, $secNum){ 
    $this->numOne = $firstNum; 
    $this->numTwo = $secNum; 
} 
public function add(){ 
    return $this->numOne + $this->numTwo; 
} 
public function subtruct(){ 
    return $this->numOne - $this->numTwo; 
} 
public function multiple(){ 
    return $this->numOne * $this->numTwo; 
} 
public function divide(){ 
    return $this->numOne/$this->numTwo; 
} 
} 
global $numOne, $numTwo; 

$resultsOne = new NumberCalculation($numOne,$numTwo); 
?> 
+0

你有什麼問題? –

+0

我的問題是,我可以輸入兩個數字,但我可以顯示輸出? –

+0

你應該在「NumberCalculation」類上傳遞一個「numOne」或「numTwo」來顯示輸出,就像你在functions.php文件底部所做的那樣 –

回答

1

你幾乎擁有它。把你的輸入您$_POST變量您NumberCalculation類的構造函數

$resultsOne = new NumberCalculation($_POST['numOne'],$_POST['numTwo']); 
echo "Added Results = ".$resultsOne->add()."<br>"; 

,如果你不想要的東西傳遞給您的NumberCalculation類的構造函數。你可以做的FF:

$resultsOne = new NumberCalculation(); 
$resultsOne->numOne = $_POST['numOne']; 
$resultsOne->numTwo = $_POST['numTwo']; 
echo "Added Results = ".$resultsOne->add()."<br>"; 
+0

我在找這個。可以告訴我'isset'和'null!=='之間的區別嗎? –

+1

'isset'檢查是否已經設置了'variable',而'null!=='只是一個簡單的比較。 – roullie

1

你沒有通過表格發佈的值。像這樣做..

<?php 
    if(null !==($_POST['numOne'] && $_POST['numTwo'])){ 
     // pass your values like below 
     $resultsOne = new NumberCalculation($_POST['numOne'], $_POST['numTwo']); 
     echo "Added Results = ".$resultsOne->add()."<br>"; 
    } 
?> 
0

添加第三個參數($funcName)以便全局使用。你只需要通過操作名稱,如加,多個劃分等

的functions.php:

class NumberCalculation{ 
      public $numOne; 
      public $numTwo; 
      private $result; 

       public function __construct($firstNum, $secNum, $funcName){ 
        $this->numOne = $firstNum; 
        $this->numTwo = $secNum; 
        $this->$funcName();     
       } 
       public function add(){ 
        $this->result = $this->numOne + $this->numTwo; 
       } 
       public function subtruct(){ 
        $this->result = $this->numOne - $this->numTwo; 
       } 
       public function multiple(){ 
        $this->result = $this->numOne * $this->numTwo; 
       } 
       public function divide(){ 
        $this->result = $this->numOne/$this->numTwo; 
       } 

       public function result(){ 
        return $this->result; 
       } 
     } 



    $numOne=3; $numTwo=2; $funcName='multiple'; 
    $resultsOne = new NumberCalculation($numOne,$numTwo,$funcName); 
    echo $resultsOne->result();