3 small changes only.
Because you have basically done it right.
echo $this->total;
... and move this echo outside of the for() loop
$i=0; $i<sizeof/B; $i++
.. because if there are 3 numbers.... [0] [1] [2]
the array is like this:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
<?php
class AddingMachine
{
private $total = 0;
function addNumbers(array $numbers)
{
for($i=0;$i<sizeof($numbers);$i++)
{
$this->total = $this->total + $numbers[$i];
}
echo $this->total;
}
}
$numObj = new AddingMachine;
$numbers = array (1,2,3);
$numObj->addNumbers($numbers);
?>