|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70457 Wrong line number for E_NOTICE - undefined constant in class
Submitted: 2015-09-08 08:46 UTC Modified: 2018-03-26 13:50 UTC
Avg. Score:3.5 ± 0.5
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:3 (75.0%)
From: daniel dot jaenecke at sedo dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: Any OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
49 + 2 = ?
Subscribe to this entry?

 [2015-09-08 08:46 UTC] daniel dot jaenecke at sedo dot com
A notice complaining about an undefined constant assigned to a class property is reported for the wrong line number.

I could reproduce this with the provided snippet for versions 5.3, 5.5 and 5.6

Test script:

class MyClass

    private $var2 = 1;


$obj = new MyClass(); 

Expected result:
A notice for the line where the undefined constant is being used, in this case Notice: Use of undefined constant NONE_EXISTING_GLOBAL_CONSTANT […] in line 6

Actual result:
Notice: Use of undefined constant ANOTHER_NONE_EXISTING_CONSTANT - assumed 'ANOTHER_NONE_EXISTING_CONSTANT' in /home/daniel/tmp/php-linenumber-bug/code.php on line 11


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-08 09:20 UTC]
-Summary: Wrong line number for E_NOTICE +Summary: Wrong line number for E_NOTICE - undefined constant in class -Package: *General Issues +Package: Scripting Engine problem -Operating System: Linux +Operating System: Any -PHP Version: 5.6.13 +PHP Version: Any
 [2015-09-08 09:20 UTC]

PHP has been emitting the error like this for a long time.
 [2015-09-08 13:54 UTC]
We currently aren't storing any line number information for class variables/constants. And as these are only evaluated upon first use, the constant message will only appear on instantiation here.
 [2017-05-19 10:58 UTC]
-Status: Open +Status: Verified
 [2018-03-26 09:41 UTC]

It happens with static var.
Error line is meaningless, since "undefined constant error" is raised for "other" static var.
It can hide "undefined constant error" with static class until code access to static var.

See 3v4l result for details.
 [2018-03-26 10:07 UTC]
Nowadays delayed constants store an AST, which contains the necessary line number information. This is still not entirely simple, as we also need a) the file name (which is available on the parent class / function, but not inside update_constant) and b) an ability to override the source of an error (something like CG(zend_lineno), but for runtime use).
 [2018-03-26 13:47 UTC]
That's what I guessed, thanks.
Anway, since we are going to raise ERROR for undefined constants (Correct?), this delayed "undefined constant error" could be serious problem. It may be better to document this behavior explicitly. define() is nasty in many ways due to its dynamic nature.

"const" works fine.
 [2018-03-26 13:50 UTC]
Oops. I thought I commented out var_dump().
const has the same issue as well.
 [2020-11-06 16:56 UTC] php4fan at gmail dot com
Congratulation guys, it's been 5 years and this is still an issue.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 23 06:01:35 2024 UTC