Skip to content

Misleading error reporting when using a non-existent constant in a static variable initializer #10356

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

Closed
TimWolla opened this issue Jan 17, 2023 · 4 comments

Comments

@TimWolla
Copy link
Member

Description

The following code (https://3v4l.org/la8tX):

<?php

final class Foo {
    public static $foo = [
        'foo' => DOES_NOT_EXIST,
    ];
}

new Foo();

Resulted in this output:

Fatal error: Uncaught Error: Undefined constant "DOES_NOT_EXIST" in /in/la8tX:9
Stack trace:
#0 {main}
  thrown in /in/la8tX on line 9

But I expected this output instead:

Some indication that the broken constant is in line 5, because line 9 clearly does not contain any constants.

PHP Version

PHP 8.2.1

Operating System

No response

@iluuu1994
Copy link
Member

iluuu1994 commented Jan 18, 2023

FWIW I started with this a while ago. https://github.com/php/php-src/pull/8855/files The exact type of constant expression is not mentioned but it at least shows the full stack trace.

Edit: Correction, it doesn't show the full constant expression trace but rather the last frame which is the one causing the error.

@TimWolla
Copy link
Member Author

The exact type of constant expression is not mentioned but it at least shows the full stack trace.

Yeah, having the correct line number would've been totally sufficient in my case. When I encountered the issue, the search for the cause was complicated by the fact that the incorrect constant expression was the static variable initializer of the parent class of the class I was instantiating with new. If I was pointed to the correct line, then the cause would've been obvious.

Thanks for the link, I've subscribed that PR.

@TimWolla
Copy link
Member Author

TimWolla commented Feb 2, 2023

@iluuu1994 I've now tested #8855 with this example. It's much better, thanks! Just one minor thing: The reported line is line 4 (which is where the const expression begins), but the constant is in line 5.

I'm not sure if this is fixable. Feel free to close this issue as resolved if it's not worth it.

@iluuu1994
Copy link
Member

@TimWolla Thanks for testing and reporting! The line number should be fixed here now too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants