Please refer this
Code input
This code doesn’t give the expected output
class User{
protected $name;
protected $age;
public function __construct($name, $age){
$this->name = $name;
$this->age = $age;
}
}
class Customer extends User{
private $balance;
public function __construct($name, $age, $balance){
$this->balance = $balance;
}
public function pay($amount){
return $this->name . ' paid $' . $amount;
}
}
$customer1 = new Customer('Adithya', 23, 50);
echo $customer1->pay(100);
It only gives this
Can someone please explain the reason?
2
Answers
Add the following line to the Customer class constructor so that the parent class constructor is called with the right parameters
So the code is as follows
(I have
added a line in the pay method
to make it more meaningful)The display will be :
(initially Adithya is having 50 as balance, but he has paid 100, so the new balance is -50)