|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-08-20 22:18 UTC] adam dot huttler at fracturedatlas dot org
Description:
------------
When defining a global constant, you can concatenate a string to another constant. Doing this with class constants causes a parse error.
If this isn't considered a bug, please consider it a feature request, as it would be very useful for configuration (especially file paths).
Thanks.
Reproduce code:
---------------
define('FOO', 'foo');
class Foobar
{
const MESSAGE = FOO . 'bar';
}
echo Foobar::MESSAGE;
Expected result:
----------------
foobar
Actual result:
--------------
Parse error: syntax error, unexpected '.', expecting ',' or ';'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 21:00:01 2025 UTC |
Maybe a solution for this issue could be using defined constants, something like this: [ START ] define('ROOT_DIR', dirname('..' . Constants::DS)); define('ACTIONS_DIRECTORY', APPLICATION_PATH . Constants::DS . 'actions' . Constants::DS); define('VIEWS_DIRECTORY', APPLICATION_PATH . Constants::DS . 'views' . Constants::DS); class Constants { const ROOT_DIR = ROOT_DIR; const DS = DIRECTORY_SEPARATOR; const ACTIONS_DIRECTORY = ACTIONS_DIRECTORY; const VIEWS_DIRECTORY = VIEWS_DIRECTORY; } [ END ] Sorry by my poor english, maybe someone could correct me...Please re-check, the behaviour is inconsistent. is inconsistent between global consts and class const. And is inconsistent between class const definition itself. [2007-08-21 06:22 UTC] derick@php.net said: "You can only initialize class constants with constant values. You pass a statement which has to be evaluated at runtime, while the class constants are defined at compile time which comes earlier." Then, it seems that something goes wrong, as I can't assign a class const to a statement but I can assign it to a const which has an statement... Which, by transitive properties :) I'm assigning an statement to it... [ START ] define('STATEMENT', 'hello' . 'world'; class Constants { const WORKS = STATEMENT; // works. const FAILS = 'hello' . 'world'; // fails. } [ END ]Additional tests, try this, according to runtime/compile blah blah, should this work? Please raise these to a feature request. <?php define('HELLO_WORLD', Constants::HELLO . Constants::WORLD); class Constants { const HELLO = 'Hello '; const WORLD = 'world.'; const HELLO_WORLD = HELLO_WORLD; } echo Constants::HELLO_WORLD;