Edit report at https://bugs.php.net/bug.php?id=62076&edit=1

 ID:                 62076
 Updated by:         f...@php.net
 Reported by:        phplists at stanvassilev dot com
 Summary:            Global/namespace constants are not hoisted
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            Scripting Engine problem
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

You can still assign a define()d constant to const, so the argument is kind of 
moot imho. 

See example below:


<?php
namespace foo;

func(); // Warning, and prints "NOT_HOISTED"

define('XXX', 'asdf.'.rand(1,9));

const NOT_HOISTED = XXX;                                                        
                                                                                
                              

func(); // prints "123"

function func()
{
    echo NOT_HOISTED." ". XXX.PHP_EOL;
}
?>

which yields:

PHP Notice:  Use of undefined constant NOT_HOISTED - assumed 'NOT_HOISTED' in 
b62076.php on line 14
PHP Notice:  Use of undefined constant XXX - assumed 'XXX' in b62076.php on 
line 
14
NOT_HOISTED XXX
asdf.2 asdf.2


Previous Comments:
------------------------------------------------------------------------
[2012-05-20 10:40:18] phplists at stanvassilev dot com

My full argument why const should be hoisted, while define() can't and 
shouldn't 
be:

const FOO = val; is a construct that doesn't depend on runtime conditions, much 
like class constants. It can't accept expressions, variables and can't be 
placed 
in conditional blocks, while define() is a function call that can accept 
variables and expressions and be placed in conditional blocks.

Therefore regardless of their opcode implementation, they are exposed in a 
fundamentally 
different way, and define() hoisting isn't expected while const hoisting is 
expected (as 
a static declaration, and similar to class const declaration, their position in 
the class DOES NOT matter).

Therefore I believe const should act like class constants, and not like 
define(), as this matches user expectations better.

------------------------------------------------------------------------
[2012-05-20 10:28:28] phplists at stanvassilev dot com

Description:
------------
"const" Declarations outside a class were introduced in PHP 5.3, and they only 
support static "compile-time" expressions, unlike define().

Function and classes declarations are hoisted to the top of the file, so you 
can 
call them before the line they are defined in. This doesn't happen 
with "const", although it's expected as a matter of consistency. This leads to 
the following odd problem: 

<?php
namespace foo;

func(); // Warning, and prints "NOT_HOISTED"

const NOT_HOISTED = 123;

func(); // prints "123"

function func()
{
        echo NOT_HOISTED;
}


Please make "const" consistent with the rest of the language, and hoist it 
together with function and class declarations.

Expected result:
----------------
To print "123" in both cases, no warning.

Actual result:
--------------
Referring to a const before the line it's declared in results in an error and 
magical string casting (as if it doesn't exist).


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62076&edit=1

Reply via email to