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

PhpClassConstants

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

The only way is to define() an expression and then use that constant in the class

define('foobar', 'foo' . 'bar');

class Foo
{
	const blah = foobar;
}

echo Foo::blah;

Another option is to go to bugs.php.net and kindly ask them to fix this.

Solution 2 - Php

Imho, this question deserves an answer for PHP 5.6+, thanks to @jammin comment

Since PHP 5.6 you are allowed to define a static scalar expressions for a constant:

class Foo { 
  const BAR = "baz";
  const HAZ = self::BAR . " boo\n"; 
}

Although it is not part of the question, one should be aware of the limits of the implementation. The following won't work, although it is static content (but might be manipulated at runtime):

class Foo { 
  public static $bar = "baz";
  const HAZ = self::$bar . " boo\n"; 
}
// PHP Parse error:  syntax error, unexpected '$bar' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS)

class Foo { 
  public static function bar () { return "baz";}
  const HAZ = self::bar() . " boo\n"; 
}
// PHP Parse error:  syntax error, unexpected '(', expecting ',' or ';'

For further information take a look at: https://wiki.php.net/rfc/const_scalar_exprs and http://php.net/manual/en/language.oop5.constants.php

Solution 3 - Php

Always fall back to the trusty manual for stuff like this.

Regarding constants:

> The value must be a constant > expression, not (for example) a > variable, a property, a result of a > mathematical operation, or a function > call.

So... "no" would be the answer :D

Solution 4 - Php

For class constants, you can't assign anything other than a constant expression. Quoting the PHP manual:

"The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. "

Solution 5 - Php

This may not be directly what you're looking for, but I came across this thread, so here is a solution that I used for an issue I was having (based off of @user187291's answer):

define('app_path', __DIR__ . '/../../');
const APPLICATION_PATH = app_path;
.
.
.
require_once(APPLICATION_PATH . "some_directory/some_file.php");
.
.
.

Seems to work great!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionselfsimilarView Question on Stackoverflow
Solution 1 - Phpuser187291View Answer on Stackoverflow
Solution 2 - PhpTrendfischerView Answer on Stackoverflow
Solution 3 - PhpPeter BaileyView Answer on Stackoverflow
Solution 4 - PhpDumb GuyView Answer on Stackoverflow
Solution 5 - PhpMuffinTheManView Answer on Stackoverflow