Collisions with other trait methods
yes this is possible you can use something like this (answer for edited content) :
class Boo {
use FooTrait, TooTrait {
FooTrait::getRow as getFooRow;
TooTrait::getRow as getTooRow;
}
public function getRow(... $arguments)
{
return [ 'foo' => $this->getFooRow(... $arguments), 'too' => $this->getTooRow(... $arguments) ];
}
public function booMethod(... $arguments)
{
return $this->getFooRow(... $arguments);
}
}
PHP Documentation about conflicts:
If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.
To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to chose exactly one of the conflicting methods.
Since this only allows one to exclude methods, the as operator can be used to allow the inclusion of one of the conflicting methods under another name.
Example #5 Conflict Resolution
In this example, Talker uses the traits A and B. Since A and B have conflicting methods, it defines to use the variant of smallTalk from trait B, and the variant of bigTalk from trait A.
The Aliased_Talker makes use of the as operator to be able to use B's bigTalk implementation under an additional alias talk.
<?php
trait A {
public function smallTalk() {
echo 'a';
}
public function bigTalk() {
echo 'A';
}
}
trait B {
public function smallTalk() {
echo 'b';
}
public function bigTalk() {
echo 'B';
}
}
class Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
}
}
class Aliased_Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
B::bigTalk as talk;
}
}
So in your case it could be
class Boo {
use FooTrait, TooTrait {
FooTrait::getRow insteadof TooTrait;
}
public function booMethod() {
return $this->fooMethod();
}
}
(it works even if you do separate use
, but i think it's more clear)
Or use the as
to declare an alias.