Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bsod85 opened this issue Feb 11, 2022 · 1 comment

Comments

@bsod85
Copy link

bsod85 commented Feb 11, 2022

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

@bsod85
Copy link
Author

bsod85 commented Feb 11, 2022

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

cmb69 added a commit to cmb69/php-src that referenced this issue Feb 16, 2022
When we need to evaluate constant ASTs, we always have to do that in
the scope where the constant has been defined, which may be a parent
of the `ReflectionClass`'s scope.
cmb69 added a commit to cmb69/php-src that referenced this issue Feb 24, 2022
When we need to evaluate constant ASTs, we always have to do that in
the scope where the constant has been defined, which may be a parent
of the `ReflectionClass`'s scope.
@cmb69 cmb69 closed this as completed in 0d266a2 Feb 28, 2022
cmb69 added a commit that referenced this issue Feb 28, 2022
* PHP-8.0:
  Fix GH-8080: ReflectionClass::getConstants() depends on def. order
cmb69 added a commit that referenced this issue Feb 28, 2022
* PHP-8.1:
  Fix GH-8080: ReflectionClass::getConstants() depends on def. order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@cmb69 @bsod85 and others