What are the values of $number1
and $number2
that you are passing in? $number1
and $number2
are not the same as $cal->number1
and $cal->number2
.
You're defining two properties of an object, and passing two distinct, separate variables into the class's function. You basically have two pairs of numbers - one pair in the object, with values of 4 and 5, and one outside the function with no values (both 0) which you are then adding.
You could try this:
<?php
class calculator {
private $number1 = 4;
private $number2 = 5;
function add ($a, $b){
$c = $this->$a + $this->$b;
print ("the sum of your numbers: $c");
print ($c);
}
}
$cal = new calculator;
$cal->add('number1', 'number2');
Or this:
<?php
class calculator {
private $number1 = 4;
private $number2 = 5;
function add (){
$c = $this->number1 + $this->number2;
print ("the sum of your numbers: $c");
print ($c);
}
}
$cal = new calculator;
$cal->add();
Or this:
<?php
class calculator {
function add ($a, $b){
$c = $a + $b;
print ("the sum of your numbers: $c");
print ($c);
}
}
$cal = new calculator;
$cal->add(4, 5);