Interface Default Methods

Read the RFC
93
169 yes
258 no

Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.

Click the bar to cast your vote!
40%
60%
1

i really like the intention of this RFC, but as i see it the method resolution is backward. If you have two Interfaces that define the same method with incompatible signatures, the first-used Interface is blamed for being incompatible with the second:

interface I1 {
    public function foo();
}

interface I2 {
    public function foo(string $bar): int;
}

// The latter interface method's signature wins the compatibility requirement:
class C implements I1, I2 { }
// Fatal error: Declaration of I1::foo() must be compatible with I2::foo(string $bar): int

class C implements I2, I1 { }
// Fatal error: Declaration of I2::foo(string $bar): int must be compatible with I1::foo()

The RFC would reverse this, keeping the method from the first-used. This also departs from replacement patterns found elsewhere, like associative arrays (the last definition of a key wins) and inheritance (an instance's members visible to the parent chain override the parents).

Share:
thookerov avatar
thookerov
voted no
1

there are abstract classes where you can exactly do this

Share:
uli avatar
uli
voted no
1

An interface defines... well.. just an interface. Not the implementation.

The feature of having default implementation is kind of already there: just use traits or abstract classes. Having any implementation in the interface makes the code blurry. When can I rely on an interface being totally a pure interface, when is it half-interface with some implementations?

It opens also a way for introducing new behavior and automatically populated to all implementations. Even to implementations where the default behavior would be plainly wrong. Wonder if my tests would show any sign of something being wrong. If I did my code based on an interface, like a contract, suddenly the contract could change without me noticing it.

Share:
daviscaics avatar
daviscaics
voted no
1

Interfaces in PHP are not "pure interfaces" (as some have pointed out here) since they can contain constants, so interfaces in PHP are partially abstract classes. The concept of a “pure interface” includes only methods. In PHP, interfaces must contain default methods.

Share:
stafred avatar
stafred
voted yes
1
  1. It would be better to allow inheritance from multiple classes as in other languages
  2. The interface should remain just an interface
Share:
harasimowiczkamil avatar
harasimowiczkamil
voted no
1

Interfaces should not contain any logic. Implementing multiple interfaces containing the same methods would also result in a new problem: which implementation should be used? Using traits, you have to choose manually using the use keyword. In this RFC no solution is provided.

Share:
ricwein avatar
ricwein
voted no
1

Interfaces should not contain logic.

Share:
marlonbasten avatar
marlonbasten
voted no
1

This RFC introduces a mess, because the implementation must be separated from the contract. As someone said: "if multi-inheritance is the subject, a specific RFC shall be done on this".

Share:
piotrfilipek avatar
piotrfilipek
voted no
1

Interface should not provide any implementation details, it's only a contract even for default.

Share:
oleg-andreyev avatar
oleg-andreyev
voted no
1

No code in Interfaces. It should be used for contracts

Share:
johnluxor avatar
johnluxor
voted no
1

It would have been a perfect solution to replace all those abstract classes that are made just as a compatibility layer between versions. And if someone doesn't like this: old way would be still working.

Share:
zmitic avatar
zmitic
voted yes
1

A big NO for this one. Interfaces are interfaces, they declare the signature and introducing a contract for the implementing classes. If you need "default" implementation, you must do that in an abstract class. Let's not confuse different things into one.

Share:
ozahorulia avatar
ozahorulia
voted no
1

Interfaces must be OO contracts without code. PHP already has the traits to implement shared methods. Mixing both wouldn't be of any help.

Share:
anibalsanchez avatar
anibalsanchez
voted no
1

I've seen that C# and Java do have this kind of logic. I've also spent a few seconds to read the blog post from brent.

Yes, there are use-cases where default methods might be helpful. But the same argument given in the blog post regarding "how many implementations will look other than..." can be given with "how many interfaces have this kind of abstraction".

So how many default methods will be around if we support this kind of "feature"? If that only applies to LoggerInterface since its design allows us to easily provide default methods, I don't see a benefit here. Most interface methods are usually not simple proxies to other interface methods and thus thats barely useful.

Share:
boesing avatar
boesing
voted no
1

Interface should be an interface, not an implementation

Share:
check24-axel-nitzsche avatar
check24-axel-nitzs...
voted no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!