Re: Pre-RFC proposal: opt-in implicit interfaces / structural typing

From: Date: Fri, 07 Nov 2025 21:34:56 +0000
Subject: Re: Pre-RFC proposal: opt-in implicit interfaces / structural typing
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-129142@lists.php.net to get a copy of this message
On Fri, Nov 7, 2025, at 1:19 PM, Rowan Tommins [IMSoP] wrote:
> On 07/11/2025 17:05, Spencer Malone wrote:
>> Hey all! Long time browser, first time emailer. I wanted to start a 
>> pre-RFC discussion on the proposal of opt-in implicit interfaces / 
>> structural typing / "golang style interfaces".
>
>
> Hi and welcome :)
>
> In general, I prefer explicit code over implicit, so wouldn't be an 
> enthusiastic supporter of this (although I might abstain rather than 
> voting against).
>
> However, I though it worth mentioning that PHP has one implicit 
> interface already: any class with an __toString() method magically 
> implements Stringable. I've never really understood why, or why that 
> interface even exists; but at least having a general concept of 
> "implicit interface" would make it less magical.

__toString() is *old*.  It may go all the way back to 5.0, but it was there at least as of 5.2.  The
problem is that there was no way to type against it.  You could type string, but not
string or object that implements string.  When we got union types in 8.0, that was
tempting but not yet possible.  So Stringable was added basically for typing against
string|Stringable.  And it was made implicitly added because there were a bajillion
__toString() functions in the wild already that shouldn't have to be modified before you could
type against them.

</history lesson>

--Larry Garfield


Thread (12 messages)

« previous php.internals (#129142) next »