Home » PHP » PHP Traits

PHP Traits

Traits are a new phenomenon in languages which do not support multiple inheritance. PHP also not directly support multiple inheritance while interface can be used to simulate but due to their abstract behavior, they are less usefull when we need functionality from two or more parent class into child class and we want to through inheritance.

Here Traits are introduced. They do exactly what multiple inheritance do, And while take care of method collision.

See following example in PHP without trait.

class Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
} 

class Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
} 

class Smartphone extends Phone
{

}

We can not inherit the functionality of Computer into Smartphone class here. As computer class is already defined and user in another world we can not convert it into interface. So we need to make Computer interface for Smartphone class need to reimplement which duplicate Computer class structure.

interface ComputerInterface
{
    public function email();
    public function surf();
    public function chat();
}

class SmartPhone  extends Phone implements ComputerInterface 
{
    public function email()
    {
        // Copy implement here from computer class
    }
    public function surf()
    {
        // Copy implement here from computer class
    }
    public function chat()
    {
        // Copy implement here from computer class
    }
}

As we see this is not efficient way some times. So we need to use design patterns to resolve this problem. But trait resolve this problem in language itself.

trait Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
} 

trait Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
}

class SmartPhone
{
    use Phone, Computer{
        Phone::login insteadof Computer;
        Computer::logout insteadof Phone;
    }
}

$smart = new SmartPhone();
$smart->login();
$smart->email();
$smart->ring();
$smart->logout();

Here we also see how trait handle same method name collision. Their are more functionality similar to classes available in trait like inheritance and abstract methods.

See (http://php.net/manual/en/language.oop5.traits.php)

Advertisements

One thought on “PHP Traits

  1. Pingback: PHP 5.4 new features | Kuldeep15's Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s