0

As far as I can tell PHP traits exist to offer pseudo multi-inheritance... and looking at them, they rather remind me of structs, except there return type is the same as functions.

ANYWAY - what can I do with a trait that I can't already do with an interface, or just another function?

aserwin
  • 1,040
  • 2
  • 16
  • 34

1 Answers1

1

A trait does not carry any type information, and therefore does not have anything in common with an interface.

class Test implements TestInterface {
   use TestTrait;
}

$x = new Test;
var_dump( $x instanceof TestInterface );  // true
var_dump( $x instanceof TestTrait );      // false

All in all, traits is a way to repeat code without resorting to copy-paste.

linepogl
  • 9,147
  • 4
  • 34
  • 45
  • Oh I see! I didn't realize that. PHP has certainly come a long way in the last couple of versions. Kind of worth using now! Thanks for the response. Helpful! – aserwin Aug 09 '13 at 15:30