php / php/php-src

Fatal error: class and trait define the same constant in the composition of child class in case constant in trait is equal to constant in child class

未关闭
#9,272 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Category: Engine Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.2k
平均合并
2 天 15 小时
30 天内合并 PR
103

描述

Description

The following code:

<?php
trait T {
    public const a = C2::b;
}
class C1 {
    use T;
    const b = 1;
}
class C2 extends C1  {
    use T;
    const b = 2;
}
echo C1::a;
echo C2::a;

Resulted in this output:

Fatal error: C1 and T define the same constant (a) in the composition of C2. However, the definition differs and is considered incompatible. Class was composed in /in/riAnX on line 9

But I expected this output instead:

22

It's not clear to me why this code is incorrect, but at the same time, the following works:

<?php
trait T {
    public const a = self::b;
}
class C1 {
    use T;
    const b = 1;
}
class C2 extends C1  {
    use T;
    const b = 2;
}
echo C1::a;
echo C2::a;

and outputs

11
PHP Version

8.2

Operating System

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

未指定源文件或测试。首先运行提供的 PHP 8.2 reproducer,并比较使用 C2::b 的 trait 常量与 self::b 变体;跟踪 trait/类常量的组合过程,以确定预期规则。完成的标准是:第一个示例遵循预期的常量解析行为,不会产生错误的 fatal error,并且有回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。