在另一个特征和类中引用相同的特征 [英] Referencing the same trait in another trait and class

查看:51
本文介绍了在另一个特征和类中引用相同的特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 似乎试图编译相同的 trait 两次.

PHP seems to be trying to compile the same trait twice.

use Behat\MinkExtension\Context\MinkDictionary;
class FeatureContext
{
    use MinkDictionary, OrderDictionary;
}

use Behat\MinkExtension\Context\MinkDictionary;
trait OrderDictionary
{
    //if you comment out this line, everything works, but methodFromMinkTrait is
    //unresolved
    use MinkDictionary;

    public function myMethod($element, $text)
    {  
       //some method that uses methods from MinkDictionary
       return $this->methodFromMinkTrait();
    }
}

编译失败,出现致命错误:

致命错误:Trait 方法 setMink 没有被应用,因为在 LunchTime\DeliveryBundle\Features\Context\FeatureContext 上与其他 trait 方法有冲突

Fatal error: Trait method setMink has not been applied, because there are collisions with other trait methods on LunchTime\DeliveryBundle\Features\Context\FeatureContext

setMink 方法仅在 MinkDictionary trait 中定义.

setMink method is only defined in MinkDictionary trait.

问题在于 OrderDictionaryFeatureContext 都使用了来自 MinkDictionary 的方法.这就是为什么我在 OrderDictionary 中添加了 use MinkDictionary.这是不允许的吗?如果您将其注释掉,那么一切正常,但编辑器显示了许多未解析的方法 - 它不知道它们来自哪里.

The problem is that both OrderDictionary and FeatureContext are using methods from MinkDictionary. That's why I added use MinkDictionary in OrderDictionary. Is this not allowed? If you comment that out, then everything works, but the editor is showing a lot of unresolved methods - it doesn't know where they are coming from.

推荐答案

当然它会编译相同的特征两次,因为您在 FeatureContext 类中使用"了 MinkDictionary 两次——第一次是在类本身中,第二次是通过 OrderDictionary.

of course it compiles the same trait twice, because you "use" MinkDictionary twice in class FeatureContext - first in the class itself and second via OrderDictionary.

只需从 FeatureContext 类中删除use MinkDictionary"语句

just remove the "use MinkDictionary" statement from the FeatureContext class

这篇关于在另一个特征和类中引用相同的特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆