Jaswinder Posted June 21, 2013 Share Posted June 21, 2013 (edited) hello friends.. i am trying to create calculator using OOPs.. its my first assignment in oops i have created a class in cal.php <?phpclass 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 June 21, 2013 by Jaswinder Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/ Share on other sites More sharing options...
DaveyK Posted June 21, 2013 Share Posted June 21, 2013 Why dont you try something out and ask us then... There are MANY resources on how to process forms with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437194 Share on other sites More sharing options...
boompa Posted June 21, 2013 Share Posted June 21, 2013 (edited) You have more than 50 posts now. How about using them 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 June 21, 2013 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437195 Share on other sites More sharing options...
Irate Posted June 21, 2013 Share Posted June 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437241 Share on other sites More sharing options...
Strider64 Posted June 21, 2013 Share Posted June 21, 2013 (edited) <?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. Edited June 21, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437243 Share on other sites More sharing options...
ignace Posted June 21, 2013 Share Posted June 21, 2013 (edited) 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 June 21, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437279 Share on other sites More sharing options...
Jaswinder Posted June 23, 2013 Author Share Posted June 23, 2013 thank you for replies.. i try to give a shot now Quote Link to comment https://forums.phpfreaks.com/topic/279415-calculator-using-oops/#findComment-1437468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.