Re: [RFC] Interface Default Methods
| From: | Deleu | Date: | Fri, 16 Jun 2023 17:50:56 +0000 |
| Subject: | Re: [RFC] Interface Default Methods | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120605@lists.php.net to get a copy of this message | ||
On Thu, Jun 15, 2023 at 12:48 AM Levi Morrison via internals <
internals@lists.php.net> wrote:
> Hello, PHP Internals,
>
> I am moving my RFC for interface default methods to discussion:
> https://wiki.php.net/rfc/interface-default-methods.
>
> This can be a useful tool for a few reasons:
> 1. It can make implementing an interface easier when certain methods
> can be implemented by other methods in the interface. For example, if
>
Countable had an isEmpty():
> bool method, it could be implemented
> by doing $this->count() > 0. Of course, if an
> implementation can be
> more efficient, they are still free to implement it how they want.
> 2. It can mitigate BC breaks in some cases. It's somewhat common for
> authors to want to expand new methods onto existing interfaces over
> time. Although this would still be a BC break, it moves it from a
> massive change (every single implementor must add something, even if
> it's a stub, or it will fail to compile) to a naming collision issue
> only (only classes which already had a method of the same name will
> fail to compile).
>
> There is prior art for this feature in both Java and C#. There may be
> other languages, but I was aware of at least these.
>
> Note that the RFC links to a partial implementation. If there are two
> or more interfaces with default methods of the same shape (name, args,
> etc) and a class implements both interfaces and doesn't provide a
> concrete implementation, which default implementation should be
> chosen? There is a proposal for resolving this in some cases which is
> modelled after Java's implementation, but it isn't implemented.
>
> Thank you for your time. I look forward to productive feedback.
>
> Levi Morrison
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
A question just occurred to me. Building up on the example of the RFC, is
the following snippet valid and would it behave as expected?
```
interface Interface1 {
function method1() { echo __METHOD__ . "\n"; }
}
interface Interface2 {
function method1() { echo __METHOD__ . "\n"; }
}
class Class1 implements Interface1, Interface2 {
function method1() {
$result = Interface1::method1();
Interface2::method1();
return $result;
}
}
$result = (new Class1())->method1();
```
--
Marco Deleu