Skip to content

Defining Array Constants in PHP Traits Causes Errors When Using Class Constants as Keys or Values #11351

Closed

Description

@zengboin

Bug report

When defining array constants within a PHP trait, if the array's keys or values utilize constants defined in the class that uses the trait, an error occurs. This issue hinders the ability to leverage class constants within trait-defined array constants, which can be a significant limitation in certain use cases.

Code snippet that reproduces the problem

https://phpstan.org/r/258a27f9-3c5a-4636-8f73-467efaeb5332

Expected output

OK

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. added this to the Easy fixes milestone on Jul 17, 2024
  2. phpstan-bot commented on Nov 30, 2024

    @phpstan-bot
    Contributor

    @zengboin After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
     PHP 8.3 (1 error)
     ==========
     
    -7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<int|string, 'CA'> should have the same value.
    +7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.
     
     PHP 8.2 (2 errors)
     ==========
     
     7: Class constants with native types are supported only on PHP 8.3 and later.
    -7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<int|string, 'CA'> should have the same value.
    +7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.
     
     PHP 7.2 – 8.1 (3 errors)
     ==========
     
     7: Class constants with native types are supported only on PHP 8.3 and later.
    -7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<int|string, 'CA'> should have the same value.
    +7: Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.
     7: Constant is declared inside a trait but is only supported on PHP 8.2 and later.
    Full report

    PHP 8.3 (1 error)

    Line Error
    7 Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.

    PHP 8.2 (2 errors)

    Line Error
    7 Class constants with native types are supported only on PHP 8.3 and later.
    7 Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.

    PHP 7.2 – 8.1 (3 errors)

    Line Error
    7 Class constants with native types are supported only on PHP 8.3 and later.
    7 Constant A::A with value array{abc: 'CA'} overriding constant TA::A with different value non-empty-array<'CA'> should have the same value.
    7 Constant is declared inside a trait but is only supported on PHP 8.2 and later.
  3. laurinkeithdavis commented on Oct 16, 2025

    @laurinkeithdavis

    Ugh, just ran into this one as well.

  4. laurinkeithdavis commented on Oct 16, 2025

    @laurinkeithdavis

    Any workaround for this as it's we just made a major change that depends and this blocking our ability to deploy an urgent change.

  5. laurinkeithdavis commented on Oct 16, 2025

    @laurinkeithdavis

    Also, why are there any errors that are "non-ignorable"?

  6. laurinkeithdavis commented on Oct 16, 2025

    @laurinkeithdavis

    I just changed my constants into functions - annoying, but works for now.

  7. added a commit that references this issue on Mar 1, 2026
    9e0d021
  8. phpstan-bot commented on Mar 1, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5115

  9. added 2 commits that reference this issue on Mar 1, 2026
    9e0d021
    e78c8bc
  10. github-actions commented on Apr 2, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  11. locked as resolved and limited conversation to collaborators on Apr 2, 2026
  12. added a commit that references this issue on Apr 7, 2026
    4b5dd5c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions