Req #73608 [Opn]: inconstistent const behavior

From: Date: Tue, 13 Dec 2016 16:17:41 +0000
Subject: Req #73608 [Opn]: inconstistent const behavior
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205962@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73608&edit=1 ID: 73608 User updated by: spam2 at rhsoft dot net Reported by: spam2 at rhsoft dot net Summary: inconstistent const behavior Status: Open Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 7.0.13 Block user comment: N Private report: N New Comment: did you look at my example? const CONST_B = 'TEST ' . CONST_A; *works* here and since one part (CONST_A) of it is inside a if-statement how can that be when "a constant can not be variable" Previous Comments: ------------------------------------------------------------------------ [2016-12-13 15:10:25] dave at mudsite dot com "because it's compile time" does not appear to exactly be a lie. The const keyword wants to be used (as is declared in the grammar) as a top-level statement. Why? Well a constant is compiled as a left, or right, node to op-codes at compile time, and therefore needs to be a literal constant. Now, imagine this code: <?php class Foo { function __toString() { return "I'm a FOO"; } } $f = new Foo(); const CONST_A = $f; const CONST_B = 'TEST ' . CONST_A; echo CONST_B . "\n"; ?> You would get a compilation fatal here as well, since a constant can not be a variable. define() however, is a runtime function that can accept variables and update the constant table with a reference to that compiled-variable(here that's an object). So looking again at your example, you're using the const-keyword within a statement dependent on a variable, therefore the value of a constant expression is variable, and a constant can not be variable. So if you want to define a constant that is variable, you have use of define() to do it at runtime. That said, just moving the grammar for T_CONST from top_statement into statement does get your code to run; however, I imagine the desire to keep const's out of places dependent on variables is the languages desire. Any internals people could feel free to correct my thoughts here. ------------------------------------------------------------------------ [2016-11-25 23:51:54] spam2 at rhsoft dot net Description: ------------ why can't you still not use 'const' inside a if-statement? "because it's compile time" is a lie which is easily to realize because otherwise CONST_B could never contain the value of CONST_A which was defined in the if-statement before [harry@srv-rhsoft:/downloads]$ cat test.php <?php $x = 1; if($x == 1) { define('CONST_A', 'A'); } const CONST_B = 'TEST ' . CONST_A; echo CONST_B . "\n"; ?> [harry@srv-rhsoft:/downloads]$ php test.php TEST A ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73608&edit=1

« previous php.bugs (#205962) next »