6

Possible Duplicate:
traits in php – any real world examples/best practices?

In what kind of situations would one use Traits in PHP? I do have a pretty good overall idea of this, but I can't seem to think of a way to use them in an application I have written, but that may be because it does not need traits at the time.

One scenario I have realized that needs traits:

  • Events. Instead of having one class that implements the observer pattern and letting all other classes inheriting it, just make it a trait and let classes that want to fire events or subscribe to use the trait. For example, the Yii framework is doing it wrong by implementing stuff at CComponent class rather than using a Trait.

Basically functionality that can be shared among classes, but may spread along multiple class hierarchies should use traits. What other scenarios could take advantage of Traits than an event system?

Community
  • 1
  • 1
Tower
  • 98,741
  • 129
  • 357
  • 507

1 Answers1

-4

The issue that Traits addresses is similar to the one that Java addresses with interfaces - how to enforce common behaviour (as represented by interfaces) among classes that are not in the same class hierarchy.

With languages such as C++ which only have inheritance, for two objects from two different classes to be used in the same context requiring the same behaviour, the two classes had to be from the same hierarchy. This sometimes meant creating quite artificial hierarchies simply to allow objects from different classes to be used in the same context.

Java tackled this problem through interfaces - an interface is essentially a contract governing the provision of behaviour so that an object of one class can be substituted for an object of a separate class because it promises the same behaviour - the interface. But they don't have to be from the same hierarchy.

PHP Traits embody this idea. A trait is a kind of interface, a set of behaviours that a class contains so that it can be used in a context that requires that behaviour. So, any Java interface example should carry over to a PHP Traits example. PHP Traits are a bit different to Java interfaces, though, since Traits can contain full function definitions, whereas Java interfaces can only contain declarations (typical PHP idiosyncrasy!)

msgmash.com
  • 1,035
  • 5
  • 10
  • 6
    PHP also has [interfaces](http://php.net/manual/en/language.oop5.interfaces.php), separately from traits...! If you want to compare apples to apples, traits are like mixins from languages such as Ruby or, well, traits from languages like Self. – deceze Feb 29 '12 at 08:16
  • 3
    I don't think Interfaces have much to do with Traits. – Tower Feb 29 '12 at 08:17
  • I'll just leave this here http://compsci.ca/v3/viewtopic.php?t=14785 – msgmash.com Feb 29 '12 at 08:52
  • Just to follow up my too-elliptical comment. You are right about PHP having both interfaces and traits, but the code that uses a class can't tell the difference between what comes from an interface, what comes from a trait and what comes from the class itself. From that perspective, a trait is nothing more than an interface that carries its own implementation. Traits are, imo, a bad idea anyway, since they increase dependencies - and with little benefit, since they have limited application. For example, they cannot access object state. – msgmash.com Feb 29 '12 at 09:27
  • They can, but not directly. You can use abstract methods for that. Any class using trait must implement such defined abstract getter/setter/any_other_state_access_function needed for trait to work. It this scenario trait abstract methods are similar to interface. – Konrad Gałęzowski Oct 18 '16 at 09:05