当类实现接口时,PHP Trait冲突

时间:2014-08-18 14:05:00

标签: php oop interface traits

我有一个实现execute方法接口的类。

接口强制execute方法有两个带有某些类型提示的参数。

此外,我还使用了具有execute方法但具有不同功能和签名的特征。我使用:

更改了特征方法名称
class MyClass implements MyInterface {

   use MyTrait 
   {
      execute as protected commanderExecute;
   }

   public function execute(SomeInterface $arg1, SomeInterface2 $arg2)
   {
       // do something
   }

}

当我尝试运行应用程序时,会抛出致命错误异常,并显示以下消息:

  

特征方法commanderExecute尚未应用,因为在...上存在与其他特征方法的碰撞...

1 个答案:

答案 0 :(得分:-1)

试试这个:

class MyClass implements MyInterface {

    use MyTrait 
    {
        MyTrait::execute as protected commanderExecute;
    }

    public function execute(SomeInterface $arg1, SomeInterface2 $arg2)
    {
        // do something
    }

}