-4

`class market{

public $count;

public $fruits;

public function __construct(){

$this->fruits=array();

$this->count=0;

}

public function shop(){

$total=0;

$this->fruits=$fruits;

foreach($this->fruits as $item){

$total+=$item->count;

}

return $total;

}

}

`This is a piece of code from an existing project,how does $total+=$item->count works here.

$total+=$item will work

what is $total+=$item->count here?

1 Answers1

0

Each item is a collection or object. It has a member (property) $count which holds the amount of fruit in a given collection. $item->count means each item has a property count!

class Fruit{

    public $count =  0;
    public $kind = "banana";

    public __construct( $kind = "banana" , $count = 0 ){
        $this->count = $count;
        $kind = $banana;
    }

    public add( $amount = 1 ){
        $count += $amount;
    }

}



...



$fruits = [];

//customer buys 20 tomatoes & 10 Bananas
$fruits[] = new Fruit( "tomato", 20 );
$fruits[] = new Fruit( "banana", 20 );

$market = new Market();

$market->shop( $fruits );

//$total = 30 items