JuanBonnett
JuanBonnett

Reputation: 786

PHP Constructors: Will an "inherited" constructor use Inherited or Parent class overriden methods?

Let's say I have a class

class Item {

public function __construct($id) {
     if(!empty($id)) {
         $this->doSomethingWithID($id);
     }
}

public function dummyMethod() {
    //does Something.
}

protected function doSomethingWithID($id) {
    //Does something with the ID
}

}

If I have an inherited class like this:

class Product extends Item {

 public function __construct() {
      parent::__construct();
 }

 protected function doSomethingWithID($id) {
      //OVERRIDES THE PARENT FUNCTION
 }

}

Will the Product class use the overridden Method? or will it use the Parent method?

Upvotes: 1

Views: 56

Answers (2)

Aidan
Aidan

Reputation: 767

You have declared a new construct method in the inherited class so it will use that, but as the inherited class calls the parent class construct method it will use that.

Upvotes: 0

Alexander
Alexander

Reputation: 632

The more specific something is, the higher priority it gets in code.

Children with methods of the same name as their parents take priority. You CAN call a parents methods by saying super.method() though.

Upvotes: 1

Related Questions