class Item {
//variable
protected $name, $price, $qty, $total;
//constructer dengan parameter
public function __construct($iName, $iPrice, $iQty) {
$this->name = $iName;
$this->price = $iPrice;
$this->qty = $iQty;
$this->calculate();
}
//method untuk menghitung
protected function calculate() {
$this->price = number_format($this->price, 2); //format 2 angka dibelakan koma
$this->total = number_format(($this->price * $this->qty), 2);//format 2 angka dibelakang koma
}
//mencetak string
public function __toString() {
return "You ordered ($this->qty) '$this->name'" . ($this->qty == 1 ? "" : "s") .
" at $$this->price, for a total of: $$this->total.";
}
}
echo (new Item("Widget 22", 4.90, 2));