Re: RFC: Trait expects interface

From: Date: Wed, 05 Jan 2022 12:36:22 +0000
Subject: Re: RFC: Trait expects interface
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-116815@lists.php.net to get a copy of this message
> 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? > > > --------------------------------------------------------------------------------------------------------------------------- > > Kirill Nesmeyanov Hello Kirill I really like to have this feature, this is not just useful for implementing interfaces, but also to enforce where a trait can be used. e.g: if ReadUserInputTrait should only be used in console commands, so putting a requirement to implement the console command interface, will ensure that the trait is not misused. However, i personally prefer the HackLang syntax of doing this. Which is: ``` trait Foo { require implements Bar; require extends Baz; } ``` This syntax also will result in no new keywords, which is always preferred, but not necessarily a requirement. ref: https://docs.hhvm.com/hack/traits-and-interfaces/trait-and-interface-requirements Regards, Saif Eddin Gmati. https://void.tn

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.internals (#116815) next »