4

This is how classes do it?

Class Main 
{
    $this->a = new A();
    $this->b = new B();
    $this->c = new C();

    $this->b->doTranslate($this->a->saySomething());
}

And this is how traits do it, not?

Class Main {
    use A;
    use B;
    use C;

    $this->doTranslate($this->saySomething());
}

I don't have much knowledge about traits at all, but by looking at new PHP 5.4 trait examples, they only seem to help in a single case. A class only be extended once to use $this together, but we can use multiple traits.

Question 1: Is it the only advantage of using traits over basic classes?

Question 2: If trait A, B, and C all have a function named example(), when I try $this->example(); how PHP is going to determine which trait will be used? What's going to happen?

Additionally, instead of writing a wall of text; just provive me a short code example with a short brief that I can have a look and undertstand. I'm not familiar with traits and don't know what they truly are.

hakre
  • 184,866
  • 48
  • 414
  • 792
Lisa Miskovsky
  • 896
  • 2
  • 9
  • 18
  • 2
    I don't believe traits are comparable to classes. I think traits are similar to interfaces, as stated here http://stackoverflow.com/questions/9495408/what-are-possible-use-scenarios-for-traits-in-php – user1477388 Mar 01 '13 at 18:53
  • 1
    Traits are reusable snippets of code that can be mixed into classes without requiring a class to `extend` a certain other class. This can be useful to reduce code duplication in independent yet similar classes. – deceze Mar 01 '13 at 19:28

2 Answers2

5

You can do many things with traits. I used it in my framework for example as Singleton and Getter/Setter.

trait Singleton
{
    protected static $_instance;

    protected function __construct(){}

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new static();
        }
        return self::$_instance;
    }
}

Another interesting use is for Aspect Oriented Programming. The answer would be to long to explain. Have a look here and here.

Question 2: If the traits have the same method than there will be a fatal error. You have to use the insteadof operator for conflict resolution. Look here

bitWorking
  • 12,151
  • 1
  • 31
  • 38
  • Q1: Can't we do the exact same thing with class (actually just renaming trait to Class in your example) then inject it into another class? It'll work pre 5.4 too. Can you show me an example where using class can't do something a trait can do? Q2: Clear enough. – Lisa Miskovsky Mar 01 '13 at 19:01
  • yes this is true..but like you said in PHP you can only extend ONE class. So with traits you can make the paradigma `don't repeat yourself`. – bitWorking Mar 01 '13 at 19:08
  • Not only this is short sighted coding-wise (and quite a bad motivation for anything), it's also not always possible this way. As outlined for example here: http://stackoverflow.com/questions/7104957/building-a-singleton-trait-with-php-5-4 – hakre Apr 26 '13 at 20:44
  • @hakre I know that Singletons (and static methods) are not the best design choice. However in my own framework I found them useful on two classes (FrontController and Config). And to build it with a trait was just a test of doability. – bitWorking Apr 26 '13 at 21:15
  • Yes those are real design problems (say hello to global static state), however I was linking foremost because of the problem in having a constructor inside the trait. I think it's worth to connect your answer with the other Q&A material over there. – hakre Apr 26 '13 at 21:37
  • @hakre yes this is quite interesting that you can break the singleton with a constructor in the using class. You never finish the training as a programmer ;) – bitWorking Apr 26 '13 at 22:11
0

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Traits have been available in PHP since 5.4. A trait is a PHP file that uses the trait keyword instead of class.One of the benefits of traits over classes and inheritance is the ability to use multiple traits inside one class. This is sometimes compared to multiple inheritance which is a feature PHP does not support.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

Read full article from here What are traits in PHP ?