Interface Default Methods

Read the RFC
95
168 yes
263 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!
39%
61%
1

I think this is very useful for PHP library developers.

They can add methods and provide a default implementation without breaking code (and if they only throw an exception). For example Doctrine could add a addAll method to the Collection interface and provide a basic implementation that uses the existing add method under the hood.

Since interfaces (in other programming languages) don't add member fields, the this won't need to be adjusted (by an offset) and the interface default methods just get added to the virtual method table (vtable) of the class, if they are not implemented by the class.

Share:
maz avatar
maz
voted yes
1

This is what an abstract class is for. Using Brents blog example, you'd create a default instance which was an abstract, this abstract would implement the default methods and the interface.

Share:
letssurf avatar
letssurf
voted no
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

Check out another RFCs

Property Hooks

A new way of interacting with properties

64
122 yes
78 no
The Pipe Operator

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

90
262 yes
122 no
new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

35
46 yes
13 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!