This doesn't work:
interface TestInterface
{
public function testMethod();
}
interface TestInterface2
{
public function testMethod();
}
class TestClass implements TestInterface, TestInterface2
{
}
Gives me the error:
Fatal error: Can't inherit abstract function TestInterface2::testMethod() (previously declared abstract in TestInterface).
Is that correct? Why is this not allowed? Doesn't make sense to me.
This also happens with abstract functions, for example if you implement an interface and then inherit from a class that has an abstract function of the same name.
The implementation of interface's members will be given by the class who implements the interface implicitly or explicitly. C# allows the implementation of multiple interfaces with the same method name.
So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.
Classes can implement more than one interface. As interface can consist of default methods in Java 8, which a class does not necessarily need to implement. We can have two interfaces that have default methods with the same name and signature.
If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error.
It appears that current PHP versions actually can do this. I've tracked the change in behavior down to this commit:
https://github.com/php/php-src/commit/31ef559712dae57046b6377f07634ad57f9d88cf#Zend/zend_compile.c
So as of php-5.3.9 the documented behavior appears to have changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With