Skip to content

Collision of interface constant and trait constant should not produce fatal error #16198

Closed
@Renji-FR

Description

@Renji-FR

Description

The following code:

<?php
trait T1
{
	final public const string C1 = 'T1';
}

interface I1
{
	public const ?string C1 = null;
	public const ?string C2 = null;
}

final class O1 implements I1
{
	final public const string C2 = 'O1';
}

final class O2 implements I1
{
	use T1;
}

abstract class A1 implements I1
{
}

final class O3 extends A1
{
	final public const string C2 = 'O3';
}

final class O4 extends A1
{
	use T1;
}

Resulted in this output:

Fatal error: I1 and T1 define the same constant (C1) in the composition of O4. However, the definition differs and is considered incompatible. Class was composed in /home/user/scripts/code.php on line 32

But I expected this output instead:

No runtime error

This bug is related to the ticket #9271.


The behavior of a trait is to copy code into class, so if we can do something without trait and without error, it should be possible to do the same thing but with trait. I don't understand why I get this error with my code.

PHP Version

PHP 8.3.9

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions