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] johan...@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

Reply via email to