Skip to content

ReflectionClass::getConstants() gives an error depending on the order of constants definition #8080

Closed

Description

@bsod85

Description

The following code:

<?php

class A {
    const LIST = [
        self::TEST => 'Test',
    ];
    private const TEST = 'test';
}

class B extends A {
    
}

$r = new ReflectionClass(B::class);
print_r($r->getConstants());

Resulted in this output:

Fatal error: Uncaught Error: Undefined constant self::TEST in /in/FuJAW:15
Stack trace:
#0 /in/FuJAW(15): ReflectionClass->getConstants()
#1 {main}
  thrown in /in/FuJAW on line 15

But I expected this output instead:

Array
(
    [LIST] => Array
        (
            [test] => Test
        )

)

that is the output you get by defining the constants this way:

private const TEST = 'test';
const LIST = [
    self::TEST => 'Test',
];

PHP Version

8.1.2

Operating System

No response

Activity

  1. bsod85 commented on Feb 11, 2022

    @bsod85
    Author

    I think that whichever outcome is correct it should be consistent and independent from the definition order

  2. added a commit that references this issue on Feb 24, 2022
    a9d0fdc
  3. added 2 commits that reference this issue on Feb 28, 2022
    27d2fdd
    c872056
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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