srinivasa Posted March 25, 2010 Report Share Posted March 25, 2010 <?php class inputs { var $no1 = $_POST['no1']; var $no2 = $_POST['no2']; function addNos() { echo $this->no1; echo $this->no2; $add = ($this->no1) + ($this->no2); echo $add; } } $obadd = new inputs(); $obadd->addNos(); ?> Quote Link to comment Share on other sites More sharing options...
administrator Posted March 25, 2010 Report Share Posted March 25, 2010 It might help if you gave us some detail ...? Quote Link to comment Share on other sites More sharing options...
krillz Posted March 25, 2010 Report Share Posted March 25, 2010 (edited) Stefan it's a class that adds two numbers together. Anyway I would suggest you try this in your code; $test = new inputs(); $test->$no2 = -9; $test->addNos(); your class is not protected to outside manipulation, now that PHP has pretty good OOP functionality I suggest you start using the proper OOP declarations for variables, classes and functions like: public, protected, private and final. Edited March 25, 2010 by krillz Quote Link to comment Share on other sites More sharing options...
srinivasa Posted March 26, 2010 Author Report Share Posted March 26, 2010 Am glad to say i got the answer........code is here class Calculator { var $sum; var $dif; function add($number1, $number2) { $this->sum=$number1 + $number2; } function substract($number1, $number2) { $this->dif=$number1 - $number2; } } $num1=$_POST['no1']; $num2=$_POST['no2']; //Create an object $calc = new Calculator(); //Call class methods $calc->add($num1,$num2); $calc->substract($num1,$num2); //Display output echo "Sum = ".$calc->sum." <br>"; echo "Difference = ".$calc->dif." <br>"; ?> now i would like to set initial value using constructor...help me out.... Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.