更新:我对这个问题的思考并不孤单,似乎确实是一个错误。见here。它修复的那一天将是一个美好的一天! :)
此操作最初为I love PHP traits! I'm going to use them everywhere! ^_^
,现在已变为Thought Exercise / Learning Experience >_<
。
考虑以下示例:
trait TheErrorOfYourWays{
public function booboo(){
echo 'You had a booboo :(';
}
}
trait SpectacularStuff1 {
use TheErrorOfYourWays;
}
trait SpectacularStuff2 {
use TheErrorOfYourWays;
}
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2;
}
这导致(显然不那么明显):
致命错误:尚未应用特质方法booboo,因为在DoSomethingSpectacular上存在与其他特征方法的碰撞。
所以我的问题:如何解决特征中的方法冲突?是否有可能实现重叠特质“继承”?如果是这样,那么“正确”的方法是什么?
为什么我要这样做:
我尝试了什么:
一系列奇妙的“as”,别名,甚至代替,在不同的地方,时间,宇宙等。包括但不限于:
trait SpectacularStuff1 {
use TheErrorOfYourWays{
TheErrorOfYourWays::booboo as booboo1;
}
}
trait SpectacularStuff2 {
use TheErrorOfYourWays{
TheErrorOfYourWays::booboo as booboo2;
}
}
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2 {
/* Tried separately, but included here for brevity's sake */
SpectacularStuff1::booboo as booboo3;
SpectacularStuff2::booboo as booboo4;
}
}
和
use TheErrorOfYourWays as Erroneous1;
trait SpectacularStuff1 {
use Erroneous1{
Erroneous1::booboo as booboo1;
}
}
use TheErrorOfYourWays as Erroneous2;
trait SpectacularStuff2 {
use Erroneous2{
Erroneous2::booboo as booboo2;
}
}
我理解:
谢谢!
答案 0 :(得分:19)
您需要使用关键字 insteadof
来解决Traits中的冲突。
重写
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2 {
/* Tried separately, but included here for brevity's sake */
SpectacularStuff1::booboo as booboo3;
SpectacularStuff2::booboo as booboo4;
}
}
到
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2
{
SpectacularStuff1::booboo insteadof SpectacularStuff2;
SpectacularStuff2::booboo insteadof SpectacularStuff1;
}
}
将解决冲突。
答案 1 :(得分:4)
所以unofficial "official" answer是:
你可以做到没有别名,替代或任何东西!但还没有......
我从5.5.1升级到5.5.6,但是'twas都是徒劳的。如果我记得,我会在修复程序可用时更新此答案。对于某种类似于解决方法的事情,有趣的是你可以直接调用特征静态函数。以下示例有效:
trait TheErrorOfYourWays{
public static function booboo($thisTrait){
echo 'You had a booboo :( in '.$thisTrait.'<br>';
}
}
trait SpectacularStuff1 {
public function boobooTest1(){
TheErrorOfYourWays::booboo(__TRAIT__);
}
}
trait SpectacularStuff2 {
public function boobooTest2(){
TheErrorOfYourWays::booboo(__TRAIT__);
}
}
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2;
}
$boobooAChoo = new DoSomethingSpectacular();
$boobooAChoo->boobooTest1(); // You had a booboo :( in SpectacularStuff1
$boobooAChoo->boobooTest2(); // You had a booboo :( in SpectacularStuff2
是的,是的,您也可以使用课程,但上个赛季的课程是 soooo 。
答案 2 :(得分:1)
我找到了另一种解决临时问题的方法:
trait A {
public function foo(){
echo 'foo';
}
}
trait B {
public function foofoo(){
return $this->foo () . 'foo';
}
}
trait C {
public function foobar(){
return $this->foo () . 'bar';
}
}
class DoSomethingSpectacular {
use A, B, C;
public function foobarfoofoo () {
echo $this->foobar () . $this->foofoo ();
}
}
它有效:)
答案 3 :(得分:0)
有点破解,只需将函数booboo
添加到课程DoSomethingSpectacular
<?php
trait TheErrorOfYourWays{
public function booboo(){
echo 'You had a booboo :(';
}
}
trait SpectacularStuff1 {
use TheErrorOfYourWays{
TheErrorOfYourWays::booboo as booboo1;
}
}
trait SpectacularStuff2 {
use TheErrorOfYourWays{
TheErrorOfYourWays::booboo as booboo2;
}
}
class DoSomethingSpectacular {
use SpectacularStuff1, SpectacularStuff2 {
/* Tried separately, but included here for brevity's sake */
SpectacularStuff1::booboo as booboo3;
SpectacularStuff2::booboo as booboo4;
}
//very ugly hack
public function booboo() {}
}