0

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?

5
  • Because you never call the parent class's construct function. Commented Nov 21, 2022 at 6:10
  • Can you please tell me how can I call it. Commented Nov 21, 2022 at 6:12
  • parent::__construct($params, $go, $here). Also, we don't accept images of code here. I already formatted your code block from earlier, but in the future you need to do that yourself. Commented Nov 21, 2022 at 6:13
  • You need to call parent class's construct function parent::__construct(); stackoverflow.com/questions/1557608/… Commented Nov 21, 2022 at 6:13
  • 2
    welcome to stackoverflow adithya! it would be best if you check on php's manual. you can call User's constructor in Customer's constructor using parent::__construct(). Commented Nov 21, 2022 at 6:14

2 Answers 2

3

Add the following line to the Customer class constructor so that the parent class constructor is called with the right parameters

parent::__construct($name, $age);

So the code is as follows

(I have added a line in the pay method to make it more meaningful)

<?php
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){
            parent::__construct($name, $age);
            $this->balance = $balance;
        }

        public function pay($amount){
            $this->balance = $this->balance - $amount;
            return $this->name . ' paid ' . $amount;
        }

        public function getbalance(){
            return $this->name . ' now has ' . $this->balance ;
        }

    }

    $customer1 = new Customer('Adithya', 23, 50);
    echo $customer1->pay(100);

    echo "<br>";

    echo $customer1->getbalance();
    ?>

The display will be :

Adithya paid 100
Adithya now has -50

(initially Adithya is having 50 as balance, but he has paid 100, so the new balance is -50)

Sign up to request clarification or add additional context in comments.

Comments

-1
    class Customer extends User{
    private $balance;

    public function __construct($name, $age, $balance){
        $this->balance = $balance;
        parent::__construct($name,$age,$balance);
    }

    public function pay($amount){
        return $this->name . ' paid $' . $amount;
    }
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.