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.
Roman Toasov
  • 747
  • 11
  • 36
  • From [documentation](http://php.net/manual/en/language.oop5.interfaces.php) I would expect multiple interfaces to be ok but not the missing `todo` in class `two`. What does the error message say? – makadev Oct 31 '16 at 00:02
  • There's no error in first example therefore i got question why child Class cannot implement same interface as parent class? – Roman Toasov Oct 31 '16 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. – makadev Oct 31 '16 at 21:27
  • Like @makadev says, there is no problem here. Only a misunderstanding of the concept `interface`. Everything is working as it should .. – dbf Nov 01 '16 at 07: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. – Roman Toasov Nov 02 '16 at 16:35

1 Answers1

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
Daniel W.
  • 31,164
  • 13
  • 93
  • 151