与其他特征方法的碰撞 [英] Collisions with other trait methods

查看:92
本文介绍了与其他特征方法的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用同名方法处理特征?

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

错误

致命错误:特性方法getRow尚未应用,因为 是与Boo中其他特质方法的冲突...

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

我该怎么办?

而且,使用两个相同的方法名称,如何从trait FooTrait获取方法?

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... 

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

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

如果我也想通过BooTooTrait获取方法getRow,该怎么办?有可能吗?

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

推荐答案

有关冲突的PHP文档:

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.

由于这仅允许一个方法排除,因此as运算符可以是 用于允许包含以下冲突方法之一 另一个名字.

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.

示例#5冲突解决方案

在此示例中,Talker使用特征A和B.由于A和B具有 冲突的方法,它定义了使用smallTalk的变体 特质B,以及特质A的bigTalk变体.

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.

Aliased_Talker利用as运算符可以使用B的 bigTalk在另一个别名讨论下的实现.

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

所以您的情况可能是

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

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

(即使您单独进行use也可以使用,但我认为更清楚)

(it works even if you do separate use, but i think it's more clear)

或使用as声明别名.

这篇关于与其他特征方法的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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