PHP wishlist/combine traits and interfaces
adapted from a 2021-08-19 post on TootCat |
Why are interface
s and traits even different things?
Interfaces
Here's everything you can do with an interface
(that I care about, anyway):
- declare
function
s - extend another
interface
- "implement" it in a class ("
implements
" keyword) - use it as a parameter type
Item 1 (functions)
This is functionally the same as defining an abstract public function
in a trait.
Item 2 (extend)
This is functionally the same as a trait use
-ing another trait
.
Item 3 (implementation)
This is functionally the same as use
-ing a trait
that consists entirely of abstract public function
s.
Item 4 (parameter type)
This is the one thing a trait
cannot do -- but I don't understand why it was done that way.
Conclusion
Combining these two ideas into a single keyword would both simplify the language and make it more powerful, I'd think.
Note
This seems related to my trait implements wishlist item from earlier. At first I thought that they might be in conflict, but on looking at the syntax in more depth, I don't see how. A trait
that uses/extends another trait
is also declaring abstract function
s in that trait
as an implementation requirement.