Jump to content

Recommended Posts

hello friends.. i am trying to create calculator using OOPs.. its my first assignment in oops

 

i have created a class in cal.php

 

<?php
class calculator
{
    public $one;
    public $two;

    public function __construct($one,$two)
    {
    $this->one=$one;
    $this->two=$two;
    }
    function sum()
    {
        $result=$this->one + $this->two;
        echo "Sum = ".$result;    
    }
    function sub()
    {
        $result=$this->one - $this->two;
        echo "Subtraction = ".$result;    
    }
    function multi()
    {
        $result=$this->one * $this->two;
        echo "Multiplication = ".$result;    
    }
    function div()
    {
        $result=$this->one / $this->two;
        echo "Division = ".$result;    
    }
    
}
?>

 

i have created a form for user input for calculator in  form.php

 

<form action="cal.php" method="post" >
Value 1 <input type="text" name="value1" />
Value 2<input type="text" name="value2" />
<input type="submit" value="Submit">
</form>

 

i dont know how to move on now??/

 

how to fetch values to next page in oops and use them in object???  can we use $_POST ??

Edited by Jaswinder
Link to comment
https://forums.phpfreaks.com/topic/279415-calculator-using-oops/
Share on other sites

You have more than 50 posts now. How about using them :code_tags:

 

 

 

how to fetch values to next page in oops and use them in object???  can we use $_POST ??

 

If you're sending them by the POST method, then you, you use $_POST. However you are just entering two numbers, no operation.

 

In the receiving page you pass the numbers to the constructor of a new object, then once you fix the problem noted in the previous sentence, you call the appropriate function on your new object.

 

 

$myObject = new MyObject($arg1, $arg2);
$myObject->doThisWithObject()
Edited by boompa

Besides, for calculators, if you don't want the page to reload at each submission of the form, why not use JavaScript? JavaScript is also an object-orientated programming language, so I believe it would very well fulfill your needs for this.

<?php
class MyCalculator {

    private $numberOne = NULL;
    private $numberTwo = NULL;
    private $operator;
    private $result;
	
    private $operators = array('+', '-', '*', '/');

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

    public function setNumberOne($numberOne) {
        $this->numberOne = $numberOne;
    }

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

    public function setNumberTwo($numberTwo) {
        $this->numberTwo = $numberTwo;
    }

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

    public function setOperator($operator) {
        $this->operator = $operator;
    }
	
    public function calculate() {
	if (isset($this->numberOne) && isset($this->numberTwo)) {
	    if (isset($this->operator) && in_array($this->operator, $this->operators)) {
		switch ($this->operator) {
	          case '+' :  $this->result = $this->numberOne + $this->numberTwo; break;
    		  case '-' :  $this->result = $this->numberOne - $this->numberTwo; break;
		  case '*' :  $this->result = $this->numberOne * $this->numberTwo; break;
		  case '/' :  $this->result = $this->numberOne / $this->numberTwo; break;
		}
				
		return $this->result;
	    }
	}
    }

}

  $calc = new MyCalculator();
  if (isset($_POST['action']) && $_POST['action'] == 'calculate') {
  $calc->setNumberOne($_POST['value1']);
  $calc->setOperator($_POST['operator']);
  $calc->setNumberTwo($_POST['value2']);
  
  $result = $calc->getNumberOne() . ' ' . $calc->getOperator() . ' ' . $calc->getNumberTwo() . ' = ' . $calc->calculate();
}
?>

<form action="my_calculator.php" method="post" >
<input type="hidden" name="action" value="calculate" />
Value 1 <input type="text" name="value1" size="6" />
<select id="basic" name="operator">
<option selected="selected" value="+">Please Select Operator</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
Value 2 <input type="text" name="value2" size="6"/>
<input type="submit" value="EQUALS">
</form>
<h3><?php echo (isset($result)) ? $result : "Example: 2 + 2 = 4"; ?></h3>

I was goofing around with this and I know I could had used a constructor, but I decided to use traditional setters and getters. I was basically just trying to refresh my memory on them and decided to use this for doing so. This is far from perfect, but like I said I was just goofing around.  :pirate:

Edited by Strider64

Strider, you should try to avoid setters/getters as much as you can.

 

abstract class AbstractCalculator {
    protected $left;
    protected $right;
    
    public function __construct($left, $right) {
        if ($left instanceof AbstractCalculator) {
            $left = $left->getValue();
        }
        
        if ($right instanceof AbstractCalculator) {
            $right = $right->getValue();
        }
        
        $this->left = $left;
        $this->right = $right;
    }
    
    public function __toString() {
        return (string) $this->getValue();
    }
    
    abstract public function getValue();
}

class Sum extends AbstractCalculator {
    public function getValue() {
        return $this->left + $this->right;
    }
}

class Multiply extends AbstractCalculator {
    public function getValue() {
        return $this->left * $this->right;
    }
}

class Divide extends AbstractCalculator {
    public function getValue() {
        return $this->left / $this->right;
    }
}
echo new Divide(new Multiply(new Sum(5, 5), 2), 10);
You should not be forced to edit the same class every time you want to add another operation. Your code becomes bloated because the class needs to do too many things. Edited by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.