Re: RFC: Trait expects interface

From: Date: Fri, 07 Jan 2022 09:00:59 +0000
Subject: Re: RFC: Trait expects interface
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-116843@lists.php.net to get a copy of this message
On 06/01/2022 23:53, Robert Korulczyk wrote:
But there is no easy way to say "FooTrait::someMethod() is implementation of FooInterface::someMethod()" that PHP and SCA will understand. And I think this proposal handles this quite well
I'm not convinced it does, actually. Consider the following trait: trait PropertyCount {
    public function count(): int {
        return count(get_object_vars($this));
    }
} This trait CAN be used to implement the built-in Countable interface, and it might be useful to label it as such; but does it really make sense to say that classes MUST implement that interface? Even if we put it as a requirement, we can't guarantee that the class will actually use the trait's implementation of the interface, because this would still be valid: class Foo implements Countable {
    private $whatever;
    use PropertyCount {
        count as getPropertyCount;
    }
    public function count(): int {
        return 0;
    }
} It feels like this use case would work better with an annotation like /** @can-implement Countable */ since it is really just documentation about possible uses. Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#116843) next »