Bug #42355 [Nab->Csd]: can't concatenate when defining class constant

From: Date: Thu, 23 Jan 2014 07:58:23 +0000
Subject: Bug #42355 [Nab->Csd]: can't concatenate when defining class constant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183966@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=42355&edit=1

 ID:                 42355
 Updated by:         nikic@php.net
 Reported by:        adam dot huttler at fracturedatlas dot org
 Summary:            can't concatenate when defining class constant
-Status:             Not a bug
+Status:             Closed
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Linux
 PHP Version:        5.2.3
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

@sdeonline: This has been implemented for PHP 5.6, see https://wiki.php.net/rfc/const_scalar_exprs.


Previous Comments:
------------------------------------------------------------------------
[2014-01-23 07:17:23] sdeonline at gmail dot com

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;

------------------------------------------------------------------------
[2014-01-23 07:10:21] sdeonline at gmail dot com

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 ]

------------------------------------------------------------------------
[2011-02-06 10:54:19] dirarck at gmail dot com

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...

------------------------------------------------------------------------
[2007-08-21 10:26:04] adam dot huttler at fracturedatlas dot org

Thanks for the reply. I suspected this would be marked bogus, since the behavior is consistent with
how object properties need to be initialized, etc.

Can this be considered a feature request? I do understand the runtime/compile time challenge. But it
seems like there may be ways around it (e.g. late static binding is getting addressed).

------------------------------------------------------------------------
[2007-08-21 06:22:09] derick@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

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.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=42355


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


Thread (7 messages)

« previous php.bugs (#183966) next »