|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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).
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 00:00:01 2025 UTC |
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