14.
Traits: "Horizontal" Reuse
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
4 Comments
Hi, I understand that if you want to explain traits, you have to use them in an example but I wondered what the difference is between the use of traits and adding another superclass above ship and bountyhuntership. Is the use of traits less OO than the latter? Is the practice of traits as accepted as adding another class to inherit from?
I presume an alternative is to create another abstract class jediShip that extends abstractShip. In that case ship and bountyhuntership are subclasses of jediship.
Hey Diederik Depourcq
Nice question, and here is what I think about traits
They were made to make code more reusable between multiple classes without affecting the system architecture, and to satisfy the need of multiple inheritance (In PHP you can inherit only from one class).
I usually don't create traits, because I use dependency injection a lot (Symfony makes it super easy), but I still think that traits can be great if you use them wisely :)
Cheers!
Ok cool. So in a way Traits wants to solve the limitation of "only one inheritance allowed". Thanks!
Hey Diederik,
Yep, some kind of it. Or, in other words, they allow you to share code (and avoid code duplications) without inheritance at all - in some cases it makes sense.
Cheers!
"Houston: no signs of life"
Start the conversation!