我该如何处理具有相同名称的特征和方法?
trait FooTrait {
public function fooMethod() {
return 'foo method';
}
public function getRow() {
return 'foo row';
}
}
trait TooTrait {
public function tooMethod() {
return 'too method';
}
public function getRow() {
return 'too row';
}
}
class Boo
{
use FooTrait;
use TooTrait;
public function booMethod() {
return $this->fooMethod();
}
}
错误,
致命错误:由于在Boo中存在与其他trait方法冲突,因此未应用getRow trait方法...
我该怎么办?
另外,当有两个相同的方法名时,如何从trait FooTrait
中获取这个方法呢?
$a = new Boo;
var_dump($a->getRow()); // Fatal error: Call to undefined method Boo::getRow() in...
编辑:
class Boo
{
use FooTrait, TooTrait {
FooTrait::getRow insteadof TooTrait;
}
public function booMethod() {
return $this->fooMethod();
}
}
如果我想通过 Boo
也从 TooTrait
获取方法 getRow
,这可能吗?
Boo
获取TooTrait
中的getRow
方法,这是否可能? - RunTooTrait::getRow as getRowOfToo;
。试试看。 - Marco Acierno