php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62076 Global/namespace constants are not hoisted
Submitted: 2012-05-20 10:28 UTC Modified: 2012-07-25 13:35 UTC
Votes:3
Avg. Score:2.3 ± 0.9
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: phplists at stanvassilev dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.4.3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phplists at stanvassilev dot com
New email:
PHP Version: OS:

 

 [2012-05-20 10:28 UTC] 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).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-20 10:40 UTC] 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-07-25 13:35 UTC] fa@php.net
-Status: Open +Status: Wont fix
 [2012-07-25 13:35 UTC] fa@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 09:01:28 2024 UTC