Can I use string concatenation to define a class CONST in PHP?

Posted by selfsimilar on Stack Overflow See other posts from Stack Overflow or by selfsimilar
Published on 2010-05-07T04:50:01Z Indexed on 2010/05/07 4:58 UTC
Read the original article Hit count: 161

Filed under:
|
|
|

I know that you can create global constants in terms of each other using string concatenation:

define('FOO', 'foo');
define('BAR', FOO.'bar');  
echo BAR;

will print 'foobar'.

However, I'm getting an error trying to do the same using class constants.

class foobar {
  const foo = 'foo';
  const foo2 = self::foo;
  const bar = self::foo.'bar';
}

foo2 is defined without issue, but declaring const bar will error out

Parse error: syntax error, unexpected '.', expecting ',' or ';'

I've also tried using functions like sprintf() but it doesn't like the left paren any more than the string concatenator '.'.

So is there any way to create class constants in terms of each other in anything more than a trivial set case like foo2?

© Stack Overflow or respective owner

Related posts about php

Related posts about const