Skip to content

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 #9272

@MaXal

Description

@MaXal

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions