Re: RFC: Trait expects interface
| From: | Larry Garfield | Date: | Wed, 05 Jan 2022 01:17:01 +0000 |
| Subject: | Re: RFC: Trait expects interface | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116805@lists.php.net to get a copy of this message | ||
On Tue, Jan 4, 2022, at 4:35 PM, Kirill Nesmeyanov wrote:
> Hello internals!
>
> Since «traits» are often an indicator of not very good code and many
> may not use them quite correctly, for example, as helpers, I suggest
> adding support for the
expects keyword to indicate that the
> trait is
> part of the code decomposition taking into account ISP.
>
> For example:
> ```
> // Definition
>
> trait LoggerTrait expects LoggerInterface
> {
> // ...
> }
>
> // Usage
>
> class MyService
> {
> use LoggerTrait; // Fatal Error: Class MyService
> expects LoggerInterface to be implemented
> }
>
> class MyService2 implements LoggerInterface
> {
> use LoggerTrait; // OK
> }
> ```
>
> How relevant do you think this idea/proposal is? And what possible
> problems or solutions will this entail in the future?
I can't say this has ever been an issue for me when using traits. I've never had a trait
that needed to be used by a class with a given interface. What I usually have is a trait that is a
full or mostly implementation of an interface. So this would be much more useful to me:
trait LoggerTrait implements LoggerInterface { ... }
(Perhaps some way to indicate that it mostly implements, and the rest are abstract methods? Or
require the other methods to be explicitly abstract?)
I think it's subtly different, as it approaches the question from the other direction.
--Larry Garfield