PHP: handling traits with methods of the same name

I have a search all over the site and Google, and I created this account ...
I need help with php, traits and classes. I have two different traits that have several methods with the same name.

The problem depends on what I need for both of them! I can not use instead ...

Here is a sample code: http://sandbox.onlinephpfunctions.com/code/b69f8e73cccd2dfd16f9d24c5d8502b21083c1a3

trait traitA {
    protected $myvar;
    public function myfunc($a) { $this->myvar = $a; }
}

trait traitB
{
    public function myfunc($a) { $this->myvar = $a * $a; }
}


class myclass 
{
    protected $othervar;

    use traitA, traitB {
        traitA::myfunc as protected t1_myfunc;
        traitB::myfunc as protected t2_myfunc;
    }

    public function func($a) {
        $this->myvar = $a * 10;
        $this->othervar = t2_myfunc($a);
    }

    public function get() { 
        echo "myvar: " . $this->myvar . " - othervar: " . $this->othervar; 
    }
}

$o = new myclass;
$o->func(2);
$o->get();

So this example ends with the obvious

Fatal error: the trait method myfunc was not used because myclass has conflicts with other trait methods

How can I solve this problem without changing the name of the method? Is it possible?

On the side of the note, this is the worst editor I've ever seen in my life!

+4
1

. . , .

use:

traitA::myfunc insteadof traitB;

( traitB::myfunc insteadof traitA;)

.

, , .

+4

Source: https://habr.com/ru/post/1540113/


All Articles