5

Is it normal behavior that child class cannot implement same interface parent class implements? I got PHP v5.6

interface blueprint {
    public function implement_me();
}

class one implements blueprint {

    public function implement_me() {

    }

}

class two extends one implements blueprint {


}

//no fatal error triggered for class two 

EDIT: So above code works great no errors or warnings even though i implemented interface blueprint in child class two without having method impement_me() why child class cannot implement same interface parent class implements?

if i implement another interface other than blueprint for class two then it works and i have to use blueprint_new methods inside class two otherwise fatal error triggered. This part works as intended.

interface blueprint {
    public function implement_me();
}

class one implements blueprint {

    public function implement_me() {

    }

}


interface blueprint_new {
    public function todo();
}


class two extends one implements blueprint_new {


}

//this will trigger fatal error.
5
  • From documentation I would expect multiple interfaces to be ok but not the missing todo in class two. What does the error message say? Commented Oct 31, 2016 at 0:02
  • There's no error in first example therefore i got question why child Class cannot implement same interface as parent class? Commented Oct 31, 2016 at 16:33
  • 1
    I think you misunderstand Interfaces. An Interface tells what members inclusive variables and respective signatures - need to be available in the implementing class - and those all its sub-classes have those members and implement the same interface. It does not say that you need to implement them even if they already exist. In the first example one implements implement_me, those two implements it already and the second implements blueprint is superfluous. Commented Oct 31, 2016 at 21:27
  • Like @makadev says, there is no problem here. Only a misunderstanding of the concept interface. Everything is working as it should .. Commented Nov 1, 2016 at 7:34
  • Was a bit late in the day i did not realize that method implement_me() was inherited from parent class one therefore no fatal error was triggered in the child. Commented Nov 2, 2016 at 16:35

1 Answer 1

9

The child class automatically inherits all interfaces from the parent class.

Sometimes you don't want this but you still can implement any, even multiple interfaces in the child class.

The only thing that does not work is to extend an interface, just like a class (or an abstract class) cannot be implemented.

The error triggered in your second code is because you didn't implement all methods from interface blueprint_new in class two, but basically there is nothing wrong in your code.

Example:

class MobilePhone implements GsmSignalPowered {}
class SamsungGalaxy extends MobilePhone implements UsbConnection {}
interface ThunderboltPowered {}
interface GsmSignalPowered {}
interface UsbConnection {}

$s = new SamsungGalaxy();
var_dump($s instanceof GsmSignalPowered); // true
var_dump($s instanceof UsbConnection); // true
var_dump($s instanceof ThunderboltPowered); // false

$m = new MobilePhone();
var_dump($m instanceof GsmSignalPowered); // true
var_dump($m instanceof UsbConnection); // false
var_dump($m instanceof ThunderboltPowered); // false
Sign up to request clarification or add additional context in comments.

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.