0

I am trying to understand one PHP OOP concept, lets say i have two classes A and B. B extends A there fore A is Base/Parent class. If class A has a __construct class B will automatically inherit it...?

Example:

class Car 
{

    public $model;
    public $price;

    public function __construct()
    {
        $this->model = 'BMW';
        $this->price = '29,00,00';
    }

}

class Engine extends Car
{
    parent::__construct();
}

By parent::__construct(); class Engine will execute Car __construct(); automatically?

But I always though if I inherit from parent class the __construct will be executed automatically anyway why would I add this parent::__construct()?

7
  • 3
    RTM Commented Jan 4, 2016 at 11:19
  • 1
    The subclass should be like this: function __construct() { parent::__construct(); } Commented Jan 4, 2016 at 11:20
  • 2
    @Thamizhan The subclass should be like this: class Engine extends Car {}. No need to override a method just to call its parent's implementation; simply omit it entirely. Commented Jan 4, 2016 at 11:21
  • 1
    Engine is not a Car.. It's a Car Component.. Commented Jan 4, 2016 at 11:23
  • @Thamizhan so hold on guys so if I simply extend the parent class, the child class will automatically execute parent class __construct when I instantiate child class....? or Do i have to add in my Child class parent::__construct...? or makes no difference and it does what are the benefits of either...? Commented Jan 4, 2016 at 11:25

2 Answers 2

2

When one class extends another, it inherits all its methods. Yes, that includes the constructor. You can simply do class Engine extends Car {}, and Engine will have a constructor and all other properties and methods defined in Car (unless they're private, which we'll ignore here).

If you define a method of the same name as already exists in Car in Engine, you're overriding that method implementation. That's exactly what it sounds like: instead of Car's implementation, Engine's method is called.

why would I add this parent::__construct()?

If you're overriding a method, yet you also want to call the parent's implementation. E.g.:

class Engine extends Car {
    public function __construct() {
        parent::__construct();
        echo 'Something extra';
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

OK las question so I guess I can override any method I wish not only __construct, if I had a method openDoor in Car class and wanted to override it in Engine class how would I do that...?
...exactly the same way...?! class Engine extends Car { public function openDoor() {} }
BTW, I hope this starts to show what @Matei was saying in the comments... An Engine with an openDoor method...!? That makes no sense. The relationship between an engine and a car is not one of inheritance, your Engine should not extend your Car. They're two different, independent classes. Your Car should have a property $engine which is an instance of Engine, if anything.
I agree I just written a quick example now looking at it I agree it makes no sense for engine to extend car xD. well Thx now I have a better picture of this idea will go and test it out now :)
0

Overriding a constructor in a child class is exactly that, overriding... you're setting a new constructor for the child to replace the parent constructor because you want it to do something different, and generally you won't want it to call the parent constructor as well..... that's why you need to explicitly call the parent constructor from the child constructor if you want them both to be executed.

If you don't create a child constructor, then you're not overriding the parent constructor, so the parent constructor will then be executed

Comments

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.