11

From what I can tell from Scala and Hack-

Mixins:

  • Can have state (ie. instance properties)
  • Can only provide concrete methods
  • Can have constructors, that are called in the same order that their classes were mixed in
  • If A mixes in B and C, A instanceof B == false and A instanceof C == false

Traits:

  • Can only provide methods, not state
  • Can declare abstract methods, that a consumer must implement
  • Cannot have constructors
  • If A implements traits B and C, A instanceof B == false and A instanceof C == false

Is this correct or am I missing anything ? Are these definitions accurate for any OO language or just for the above mentioned ones ?

Christophe
  • 81,699
bcherny
  • 263

2 Answers2

2

PHP does not have concept of mixins, however it has traits which look like mix of traits and mixins from hack/scala:

  • Can define properties.
  • Can define constructors.
  • Can define abstract methods.
  • Does not support inheritance.
  • Traits are not types.

Are these definitions accurate for any OO language or just for the above mentioned ones ?

I don't think that there is even a single thing that will work in the same way in all OO languages. Even some basics like inheritance and interfaces have some variations, not mention more blurred constructs like traits/mixins.

rob006
  • 146
0

Traits are static access in PHP as explained by Benjamin Eberlei on his blog. They are similar to automated copy-paste of code.

This does not mean that traits are absolutely evil, but they most of the time impose coupling issues.

Mixin in general is the usual (somewhat safe) use of multiple inheritance. Languages with multiple inheritance and by-ref (duck) typing like Python offer elegant uses for mixins.

abstrus
  • 156