Bug #51765 [Bgs]: class constant has different setter behavior than global constant
| From: | colin at sheaff dot net | Date: | Fri, 07 May 2010 15:52:04 +0000 |
| Subject: | Bug #51765 [Bgs]: class constant has different setter behavior than global constant | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-150248@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51765&edit=1
ID: 51765
User updated by: colin at sheaff dot net
Reported by: colin at sheaff dot net
Summary: class constant has different setter behavior than
global constant
Status: Bogus
Type: Bug
Package: Class/Object related
Operating System: Any
PHP Version: Irrelevant
New Comment:
And yet the following works:
define( 'FOO', 'foo' );
define( 'BAR', FOO . 'bar' );
class ThisWorks {
const bar = BAR;
}
echo ThisWorks::bar;
If class constants are really compile time dependent, why is it ok to
set one to
a global const value which is only evaluated at run time?
Previous Comments:
------------------------------------------------------------------------
[2010-05-07 16:55:20] johannes@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
This is a limitation in the implementation. For the class constant we
need a constant value at compile time and can't evaluate expressions.
define() is a regular function, evaluated at run time and can therefore
contain any value of any form.
changing this would mean to add an execution phase in the compiler ...
------------------------------------------------------------------------
[2010-05-07 15:17:53] colin at sheaff dot net
Description:
------------
Global constants can be set using string concatenation and other global
constants. This is useful.
Class constants cannot be set using string concatenation or any
functions, although they can be set to a global constant, or another
class constant. The difference in setter behavior is confusing and
limits utility of class constants.
Test script:
---------------
define( 'FOO', 'foo' );
define( 'BAR', FOO . 'bar' );
echo BAR . PHP_EOL;
class Foobar {
const foo = 'foo';
const bar = self::foo . 'bar';
}
echo Foobar::bar;
Expected result:
----------------
foobar
foobar
Actual result:
--------------
Parse error: syntax error, unexpected '.', expecting ',' or ';' on
line
8
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51765&edit=1