1

I have a controller that uses two traits, CavityTools and OperationTools

class OperationController extends Controller
{

    use \App\Traits\CavityTools;
    use \App\Traits\OperationTools;

However, the second trait OperationTools usingCavityTools`:

trait OperationTools {
    use \App\Traits\CavityTools;

So when I try to use a method of OperationTools from any controller's method such as $this->getProduction(), I have got an error tells about a method in the CavityTools that it is not applied due to collision:

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

I have tried to to alias the second trait, use \App\Traits\OperationTools as OpTs; but it generate parse error:

Parse error: syntax error, unexpected 'as' (T_AS), expecting ',' or ';' or '{'

How could I solve this issue?

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • 1
    Why don't you just use the `OperationTools` trait, since it uses `CavityTools` already inside your controllers? `class Ctrl { use OperationTools; //rest of the class }` see example http://sandbox.onlinephpfunctions.com/code/00d35ec51050fab3f5ebc1348b7bebf8ec720f5d – Kyslik Apr 06 '18 at 12:35
  • @Kyslik because there are another controller's actions uses another methods of `CavityTools` directly. However, it is good idea, I will check it soon. – SaidbakR Apr 06 '18 at 12:39
  • 1
    When interpreting code traits are just copied inside the class, there is nothing special about them its kind of same concept like *extending* class. I fail to understand your latest comment. I am also surprised that you have 7k rep and mostly from PHP and do not know this. Really weird. – Kyslik Apr 06 '18 at 12:41
  • Your comment make it clear, I mistakenly realized that each trait may works as isolated capsule but I did not think that it works like PHP include, i.e each trait that use another trait, it will bring its methods with it. – SaidbakR Apr 06 '18 at 12:45
  • you could rename one of them checkout this example https://stackoverflow.com/questions/39441612/php-trait-override-protected-trait-method/39441990#39441990 – Sari Yono Apr 06 '18 at 12:46

2 Answers2

2

Just use the OperationTools trait since CavityTools are already used.

Example code:

<?php


trait A {
    function a() {
        echo "a trait\n";
    }
}

trait B {
    use A;
    function b() {
        echo "b trait\n";
    }

    function a() {
        echo "a fcn from trait B\n";
    }
}

trait C {
    use B;
    function a() {
        echo "a fcn from C trait\n";
    }

    function b() {
        echo "b fcn from C trait\n";
    }
}


class AClass {
    use A;
}

$classA = new AClass;
$classA->a();
// $classB->b(); // will throw up


class BClass {
    use B;
}

$classB = new BClass;
$classB->a();
$classB->b();

class CClass {
    use C;
}

$classC = new CClass;
$classC->a();
$classC->b();

// output

a trait
a fcn from trait B
b trait
a fcn from C trait
b fcn from C trait
Kyslik
  • 8,217
  • 5
  • 54
  • 87
1

This is because there are same functions in both traits. To avoid that you will have to use "InsteadOf" in your current class.

Reference - Collisions with other trait methods