I have this code. What I want to do is echo the return value of getPrice at the bottom of the code. First I created a setPrice function with certain conditions. According to those conditions the out put should be 750, but it keeps returning 600.
Below this code, I copy a second code which outputs the correct number, but using a different logic.
Hope you can help me!
1st CODE (does not work)
class book
{
public $format = ['HARD', 'POCKET'];
public $price;
public function __construct($format,$price)
{
$this->format = $format;
$this->price = $price;
}
public function setPrice()
{
if ($this->format== 'HARD') { $this->price= $price+ $price * 1/4;
}
else { $this->price= $price;
}
}
public function getPrice()
{
return $this->price;
}
}
$book1 = new book('HARD',600);
echo $book1->getPrice();
2nd CODE:(works). No get function. Only set with 'return' expressions.
class book
{
public $format = ['HARD', 'POCKET'];
public $price;
public function __construct($format,$price)
{
$this->format = $format;
$this->price = $price;
}
public function setPrice()
{
$pricetotal;
if ($this->format == 'HARD') {$pricetotal = $this->price + $this->price * 1/4;
return $pricetotal;}
else {return $this->price ;}
}
$book1 = new book('HARD',600);
echo $book1->setPrice();