【问题标题】:PHP Trait conflict when class implements interface类实现接口时的PHP Trait冲突
【发布时间】:2014-08-18 14:05:00
【问题描述】:

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

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

我还使用了一个具有execute 方法的特征,但具有不同的功能和签名。我使用以下方法更改特征方法名称:

class MyClass implements MyInterface {

   use MyTrait 
   {
      execute as protected commanderExecute;
   }

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

}

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

Trait 方法 commandExecute 没有被应用,因为与其他 trait 方法在 ...

【问题讨论】:

  • Can not reproduce(另一个更有用的示例是here
  • 请添加 MyTrait 和 MyInterface 代码。
  • 无法复制;将trait MyTrait {public function execute(){}} interface MyInterface{} class MyClass implements MyInterface {use MyTrait{execute as protected commanderExecute;}public function execute(){}} 粘贴到 PHP shell 中不会导致错误。
  • 由于无法复制而投票关闭。您需要向我们提供足够的代码才能真正导致错误消息。
  • @AlmaDo 你的链接都指向我的“hello world”程序。

标签: php oop interface traits


【解决方案1】:

试试这个:

class MyClass implements MyInterface {

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多