OOP Intermezzo: Traits

Published on Aug 06, 2011 by Pim Elshoff

Technology #PHP #PHP 5.4 #Traits #OOP #Concept

Recently the first previews of PHP 5.4, with several cool new features, has been released. I want to talk about an OOP concept that will be introduced in this version of PHP: traits.

For a complete reference on traits, please see the php rfc wiki.

What are traits?

I referenced traits earlier when talking about interfaces vs. abstract classes. Traits solve the problem of un-hierarchical code re-use or horizontal code re-use. In English: Traits allow you to implement functionality that can be used in classes, without binding that class to a location in the inheritance tree. Still not English enough? Alright, let’s look at an example.

trait FlightCapability
{
      function fly()
      {
            ...
      }
}

class Bird use FlightCapability { ... }

class Bat extends Mammal use FlightCapability { ... }

Perhaps not the most exciting of examples, but the idea is that even though both Birds and Bats are not really related, they can still both fly. Why implement the flight functionality twice when you can define a trait and use it in both flying animals?

You can even inherit methods from more than one trait, pick which methods you want to include and alias the methods to inherit with a new name.

How do traits fit in OO

If interfaces are a way of defining a contract only, without tying the implementing class into a node in the inheritance tree, traits are a way of implementing functionality without tying in. They serve no polymorphic mechanism, but instead provide a simple and elegant solution to sharing methods over a variety of classes without further implications.

Traits in other languages

The idea of traits is not new, even though it is only now available to PHP. Several similar approaches have been tried out in the past, though some differed slightly. Traits are often used synonymously to mixins and even though they are not exactly the same the ideas are very similar.

Traits and/or mixins are available in JavaScript, Python, Ruby, a host of other languages and soon PHP.

Would I use it?

In theory traits seem very nice and I do believe that I could think of some situations where I would use traits. But those situations are not really numerous right now and in the situations where I would use them there may very well be different possible solutions. But perhaps once the possibility opens up, I need to change my way of thinking and work towards solutions that allow for traits to become an integral part of programming PHP. What do you think? Would you use traits? Are there other up and coming PHP 5.4 features that excite you?

Pim Elshoff

About the author

Pim has been working the web since 2004! Read more about Pim

Comment(s)

  1. Ivink

    Ivink said:

    Based on my work experience, I pasmythized with the Visual Basic one:You do a Google search on how to shoot yourself in the foot using VB . You find seventeen completely different ways to do so, none of which are properly structured. You paste the first example into the IDE and compile. It brushes your teeth. 11:51, 16 April 2012
  2. pftzqcwa

    pftzqcwa said:

    8mr7Mz vlwycvhyylmr 06:13, 17 April 2012
  3. ybtastxgj

    ybtastxgj said:

    a94PZF , [url=http://xibglpojkhfp.com/]xibglpojkhfp[/url], [link=http://aasoxnsqajbj.com/]aasoxnsqajbj[/link], http://uhpqiiorzgnc.com/ 07:39, 18 April 2012
  4. sbxdklcbt

    sbxdklcbt said:

    G1g5qY , [url=http://kuhvjdzqzlec.com/]kuhvjdzqzlec[/url], [link=http://fdwfbcqjcitk.com/]fdwfbcqjcitk[/link], http://urrgjaziseqm.com/ 19:26, 19 April 2012

Trackbacks

No trackbacks yet

Leave a comment

All comments will be moderated

  Veld is verplicht
Captcha
  I'm terribly sorry that this is necessary and I appreciate the effort you are taking to post a comment!