Cannot implement two interfaces that have the same method name

后端 未结 5 1983
[愿得一人]
[愿得一人] 2021-01-13 07:28

This doesn\'t work:

interface TestInterface
{
    public function testMethod();
}

interface TestInterface2
{
    public function testMethod();
}

class Test         


        
5条回答
  •  星月不相逢
    2021-01-13 07:54

    It makes no sense to implement two interfaces containing methods with the same signatures.

    The compiler cannot know if the methods actually have the same purpose - if not, it would mean that at least one of the interfaces cannot be implemented by your class.

    Example:

    interface IProgram { function execute($what); /* executes the given program */ }
    interface ISQLQuery { function execute($what); /* executes the given sql query */ }
    
    class PureAwesomeness implements IProgram, ISQLQuery {
        public function execute($what) { /* execute something.. but what?! */ }
    }
    

    So as you see, it's impossible to implement the method for both interfaces - and it'd also be impossible to call the method which actually implements the method from a given interface.

提交回复
热议问题