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

 ID:                 62076
 User updated by:    phplists at stanvassilev dot com
 Reported by:        phplists at stanvassilev dot com
 Summary:            Global/namespace constants are not hoisted
 Status:             Open
 Type:               Bug
 Package:            Scripting Engine problem
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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