duanche4578 2014-07-31 16:38
浏览 45
已采纳

与其他特质方法的碰撞

How can I deal with traits with methods of same name?

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();
    }
}

error,

Fatal error: Trait method getRow has not been applied, because there are collisions with other trait methods on Boo in...

What should I do about it?

And also, with two same method names, how can I get the method from trait FooTrait?

$a = new Boo;
var_dump($a->getRow()); // Fatal error: Call to undefined method Boo::getRow() in... 

Edit:

class Boo
{
    use FooTrait, TooTrait {
        FooTrait::getRow insteadof TooTrait;
    }

    public function booMethod() {
        return $this->fooMethod();
    }
}

what if I want to get the method getRow from TooTrait via Boo as well? Is it possible?

  • 写回答

1条回答 默认 最新

  • dtj88302 2014-07-31 16:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码