How To Solve There Are Collisions With Other Trait Methods In PHP

Hello, devs,

In this PHP traits tutorial, I will discuss a topic that is related to PHP traits. Before starting this PHP traits example, I am assuming that you know what is PHP trait is. In this example, I will discuss a situation that h what you will do when you have to call a function that exists both traits?

Let assume you have a Trait A and another Trait B. But A and B both trait contains the same method i_love_coding() and you need to call it some other class. What you will do? Let's see an example of it and see what happens.

Let assume we have a Tea trait like below:

App\Helper\Tea.php

namespace App\Helper;

Trait Tea
{
    public function i_love_drink(string $drink) : string
    {
        return $drink;
    }
}

 

And we have another Trait Coffee like below:

App\Helper\Coffee.php

namespace App\Helper;

Trait Coffee
{
    public function i_love_drink(string $drink) : string
    {
        return $drink;
    }
}

 

Now look, our both trait contains a method name i_love_drink() and which just return a string. Now what happens if we call it like below:

namespace App\Http\Controllers;

use App\Helper\Tea;
use App\Helper\Coffee;

class TestController extends Controller
{   
    
    use Coffee, Tea; 
    
    public function index()
    {    
        return $this->i_love_drink('I love coffee!');
    }
  
}

 

Now, do you know what happens? If we run this code we will see this following errors like:

Trait method i_love_drink has not been applied, because there are collisions with other trait methods on App\Http\Controllers\TestController

 

So, what have we to do if we want to solve this situation? See the below example to solve this situation.

namespace App\Http\Controllers;

use App\Helper\Tea;
use App\Helper\Coffee;

class TestController extends Controller
{   
    
    use Coffee {
        Coffee::i_love_drink insteadof Tea;
    }

    use Tea;
    
    public function index()
    {    
        return $this->i_love_drink('I love coffee!');
    }
   
}

 

Now if you run this code, you will see the following output:

 

Output
I love coffee!

 

Now assume a scenario where you have to run both methods from the same class. What you will do? If you want to solve this situation, see the following example:

namespace App\Http\Controllers;

use App\Helper\Tea;
use App\Helper\Coffee;

class TestController extends Controller
{   
    
    use Coffee {
        Coffee::i_love_drink insteadof Tea;
    }

    use Tea {
        Coffee::i_love_drink as i_love_tea;
    }
    
    public function index()
    {    
        $data = [
            'tea'    => $this->i_love_tea('I love tea!'),
            'coffee' => $this->i_love_drink('I love coffee!'),
        ];

        return $data;
    }
}

 

Now if you run this code, you will see the following output:

{
   "tea": "I love tea!",
   "coffee": "I love coffee!"
}

 

Recommened: When to Choose Composition Over Inheritance in PHP

 

Hope this PHP trait tutorial will help you to learn new things.

 

#php