Re: [PHP-DEV] [RFC] Adding a "Stringable" interface to PHP 8

This is only part of a thread. view whole thread
  108273
January 27, 2020 15:55 larry@garfieldtech.com ("Larry Garfield")
On Mon, Jan 27, 2020, at 10:19 AM, Nikita Popov wrote:
> On Wed, Jan 22, 2020 at 4:47 PM Nicolas Grekas grekas+php@gmail.com> > wrote: > > > Hello everyone, > > > > as announced last week, I'm officially opening a discussion for adding a > > "Stringable" interface to PHP 8. > > > > The RFC and its rationale are presented here as required: > > https://wiki.php.net/rfc/stringable > > > > The patch is found on GitHub, where some discussions happened already, you > > might be interested in having a look: > > https://github.com/php/php-src/pull/5083 > > > > TL;DR, I think we need an interface to allow passing objects with a > > __toString() method in a type-safe way, using the string|Stringable type. > > Here is the stub of the proposal: > > interface Stringable { public function __toString(): string; } > > > > I invite everyone to review the RFC before commenting here as I tried to > > sum up considerations that some already had before and that you might have > > too. > > > > Cheers, > > Nicolas > > > > I'm wondering whether it could make sense to automatically implement this > interface for all classes that define __toString(). Explicitly implementing > the interface is useful for the transition period (where old PHP versions > also need to be supported), but when considering only PHP 8 support, I > think it may make sense to implicitly implement the interface. If the > __toString() functionality also works fine without implementing the > interface, and there's probably only going to be relatively few consumers > of the Stringable type, it will likely be quite common that the interface > will not get implemented... > > Automatically implementing interfaces based on implemented methods could > also be a general language feature (see Go interfaces for example), but > likely not one we're interested in having in it's full generality. In this > particular case, it seems useful though. > > Regards, > Nikita
I believe Anthony Ferrara proposed "Weak interfaces" once many years ago, which is essentially what you're describing. It didn't get much traction although I'm unclear why. (Possibly just less familiarity with Go at the time?) I agree not all interfaces should be implicit, but having implicitly-implementable interfaces does sound generally useful. That shouldn't block this RFC, but might this RFC be a model for how to implement such things more generically in the future? --Larry Garfield
  108277
January 27, 2020 19:20 mike@newclarity.net (Mike Schinkel)
> On Jan 27, 2020, at 10:55 AM, Larry Garfield <larry@garfieldtech.com> wrote: > > On Mon, Jan 27, 2020, at 10:19 AM, Nikita Popov wrote: >> Automatically implementing interfaces based on implemented methods could >> also be a general language feature (see Go interfaces for example), but >> likely not one we're interested in having in it's full generality. In this >> particular case, it seems useful though. >> >> Regards, >> Nikita > > I believe Anthony Ferrara proposed "Weak interfaces" once many years ago, which is essentially what you're describing. It didn't get much traction although I'm unclear why. (Possibly just less familiarity with Go at the time?) > > I agree not all interfaces should be implicit, but having implicitly-implementable interfaces does sound generally useful. That shouldn't block this RFC, but might this RFC be a model for how to implement such things more generically in the future? > > --Larry Garfield
Glad to see you picked up on this Larry. I don't think "Weak Interfaces" became an RFC unless Google is failing me, but I found a reference to it being on the mailing list from Matthieu Napoli's article on GitHub about decoupling, which is worth a read: https://github.com/mnapoli/mnapoli.github.io/blob/master/articles/decoupling-packages.md As far as Go's implicit interfaces, I have found that to be one of the most compelling language features of Go, and would be ecstatic if we could find a way to support implicit interfaces in PHP. -Mike