-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow traits to implement interfaces #1773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Traits can now declare one or more interfaces. The trait must implement each of the methods from all the interfaces it implements with compatible declarations. Failure to do so is a fatal error. The interfaces declared by a trait do not affect classes that use the trait. Implements Proposal 1 of https://wiki.php.net/rfc/traits-with-interfaces
I mostly like this idea of implementing an interface in a trait. By there is one pitfall: whenever you rename methods used from trait (http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits.conflict) , resulting class may not implement specified interface anymore. |
This would be a really interesting feature for us!
This is already the case for classes, when one says it implements a given interface and at the end it does not. This should raise a PHP error. |
@betaglop such features will only be introduced in PHP.next, which currently is 7.2 as per our policy :-) |
nice thx for this answer. |
@kevingessner any update on this one? Would love to see this feature in PHP. Maybe start by reviving the discussion on internals? |
Is there is a possibility to see this is 7.x? |
Found the RFC for this while researching design patterns that help ensure a contract between Interface and relevant Traits within the current functionality. One thing in particular, a class executing some kind of verification to register visibility of methods required by an interface, which are provided by relevant Traits. An ignition system, of sorts, so the class can go about its business knowing the Trait/Interface contract is secured. (If anyone has suggestions where i can observe and learn how to build such a design structure, it would be appreciated.) A couple thoughts on the RFC:
The implication is each Trait is providing expected logic according to its own requirements/responsibilities, but the collision requires a resolution: only 1 of them can win the i really hope the RFC gets some traction. This would be great for 7.3 (and would have been for 7.1 and then 7.2...) 😉 Addendum: |
Comment on behalf of kalle at php.net: Closing due to no activity, please re-open if you still intend on working on it |
Traits can now declare one or more interfaces. The trait must implement
each of the methods from all the interfaces it implements with
compatible declarations. Failure to do so is a fatal error.
The interfaces declared by a trait do not affect classes that use the
trait.
Implements Proposal 1 of https://wiki.php.net/rfc/traits-with-interfaces