php / php/php-src

Collision of interface constant and trait constant doesn't produce fatal error

Open
#9,271 13 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Engine Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php

trait MyTrait{
    const A = 1;
}
interface I{
    const A = 2;
}

class D implements I{
    use MyTrait;
}

echo D::A;

Resulted in this output:

1

But I expected this output instead:

Fatal error: I and MyTrait define the same constant (A) in the composition of D. However, the definition differs and is considered incompatible. Class was composed in /in/EVuKh on line 10
PHP Version

8.2

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the PHP 8.2 reproduction script from the issue and confirm that the trait and interface constants resolve to 1 instead of producing an error. Trace the trait/interface constant composition for class D, then add a regression test showing the expected fatal error and verify the existing behavior is preserved for compatible definitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.