加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

为什么方法重命名在PHP特征中不起作用?

发布时间:2020-12-13 13:07:49 所属栏目:PHP教程 来源:网络整理
导读:我使用 PHP 7.1.0. 假设我们有一个特征,我们在一个类中使用它并重命名导入的方法: trait T{ public function A() { echo "."; }}class C{ use T { A as B; }}$c = new C();$c-B();$c-A(); // Why does it work? 为什么PHP仍允许我使用旧方法名称(在本例中为
我使用 PHP 7.1.0.

假设我们有一个特征,我们在一个类中使用它并重命名导入的方法:

trait T
{
    public function A() {
        echo ".";
    }
}

class C
{
    use T {
        A as B;
    }
}

$c = new C();
$c->B();
$c->A(); // Why does it work?

为什么PHP仍允许我使用旧方法名称(在本例中为A)?

这真的很痛苦,因为在更复杂的示例中,您不能依赖于方法重命名 – 因此您可能会意外地收到“不兼容的声明”错误:

class BaseSrc
{
}

trait BaseTrait
{
    public function init(BaseSrc $baseSrc)
    {
        echo "Init Base";
    }
}

class Base
{
    use BaseTrait {
        BaseTrait::init as initBase;
    }
}

$base = new Base();
$base->initBase(new BaseSrc());
$base->init(new BaseSrc()); // WHY DOES IT WORK?????

class MainSrc extends BaseSrc
{
}

trait MainTrait
{
    use BaseTrait {
        BaseTrait::init as initBase;
    }

    public function init(MainSrc $mainSrc)
    {
        $this->initBase($mainSrc);
        echo "Init Main";
    }
}

// Warning: Declaration of MainTrait::init(MainSrc $mainSrc) should be compatible with Base::init(BaseSrc $baseSrc)
class Main extends Base
{
    use MainTrait;
}

我认为,这段代码应该可行.由于我在Base类中将init()重命名为initBase()并在MainTrait中使用BaseTrait时进行相同的重命名,我希望此方法(BaseTrait :: init())不会与MainTrait :: init()冲突.事实上,PHP说我有不兼容的声明.其背后的原因是将init重命名为initBase不起作用 – 在我的Base类中,方法init仍然存在!

有没有办法如何解决这个问题,而不是从一开始就将BaseTrait :: init()重命名为BaseTrait :: initBase()(不仅仅是在use语句中)?

我应该将此视为PHP错误并报告吗?这种行为背后有什么合理的吗?

正如评论中所述和完整性;从 PHP manual section on Traits:

The Aliased_Talker makes use of the as operator to be able to use
B’s bigTalk implementation under an additional alias talk.

然后:

The as operator can be used to add an alias to one of the methods.
Note the as operator does not rename the method and it does not affect
any other method either.

因此,添加别名但不会以任何方式替换或影响原始方法.这是预期的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读