1

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?

2
  • 1
    You could try it out and see Commented Jan 13, 2014 at 3:23
  • Had no way, because I was working on someone's else code and didn't find a safe way to test, just wanted to make sure it works the way I think so I can keep on analysing it. Thanks Commented Jan 13, 2014 at 3:32

2 Answers 2

1

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.

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

3 Comments

That's right. If the children's constructor uses a method that is overridden in its class it won't use the parent's method.
Sorry, did I misunderstand your question?
No, I'm giving you the reason, you're right when you say Children's methods take the priority.
0

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.

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.