PHP wishlist/combine traits and interfaces

From Woozle Writes Code
Jump to navigation Jump to search
adapted from a 2021-08-19 post on TootCat

Why are interfaces and traits even different things?


Here's everything you can do with an interface (that I care about, anyway):

  • declare functions
  • 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 functions.

Item 4 (parameter type)

This is the one thing a trait cannot do -- but I don't understand why it was done that way.


Combining these two ideas into a single keyword would both simplify the language and make it more powerful, I'd think.


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 functions in that trait as an implementation requirement.