Re: [VOTE] Interface Default Methods
| From: | Larry Garfield | Date: | Tue, 11 Jul 2023 14:36:09 +0000 |
| Subject: | Re: [VOTE] Interface Default Methods | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120776@lists.php.net to get a copy of this message | ||
On Tue, Jul 11, 2023, at 2:04 PM, Robert Landers wrote:
>> Not exactly, How you wanna solve by abstract class two interfaces
>> which can be implemented using let's say two traits - let's say
>>
>> interface Foo {
>> public function foo(): string;
>> }
>> trait HasFoo {
>> public function foo(): string { return 'foo'; }
>> }
>> interface Bar {
>> public function bar(): bool;
>> }
>> traitHasBar {
>> public function bar(): bool { return true; }
>> }
>>
>> Now I can need to implement Foo or Bar separately or together.
>> Using abstract class that would require 3 abstract classes: Foo, Bar, and FooWithBar.
>> With this RFC that would require just two interfaces with default methods.
>
> This seems like a software design issue, not a language issue...
>
> But 3 abstract classes vs. 2 interfaces + 2 traits seems like a better
> tradeoff if you're going to need the coupling anyway. It's more
> obvious that you want/require FooWithBar when you want it. If you're
> expecting these exact implementations, moving them to the interface
> makes it even more confusing.
>
> I want FooWithBar (iow, I want these exact implementations)
>
> I want Foo&Bar (iow, I don't care about the implementation)
>
> You can pass a FooWithBar to Foo&Bar, but you can't pass a Foo&Bar to
> a FooWithBar.
>
> If the default implementation is on the interface ... who knows what
> you have, you'll have to check you /vendor folder.
You have to check your vendor folder for an interface, or trait, or abstract class. That
doesn't change anything.
As noted, now consider a Baz interface. For interface-and-trait, you add 2 more definitions. For
interface-with-defaults, you add 1. For abstract classes, you need to add 4: BazBase, FooBazBase,
BarBazBase, and FooBarBazBase.
And that's assuming you're even in control of the base classes; you may not even be able
to create all those combinations without lots of copy-pasta.
Abstract classes are basically vestigial since PHP 5.4, and have no use cases:
https://www.garfieldtech.com/blog/beyond-abstract
--Larry Garfield