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?

5
  • 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 sandbox.onlinephpfunctions.com/code/… Commented Apr 6, 2018 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. Commented Apr 6, 2018 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. Commented Apr 6, 2018 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. Commented Apr 6, 2018 at 12:45
  • you could rename one of them checkout this example stackoverflow.com/questions/39441612/… Commented Apr 6, 2018 at 12:46

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.