Is it possible to use string concatenation to define the CONST class in PHP?

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 get an error message trying to do the same using class constants.

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

foo2 is detected without problems, but declaring a const bar will result in an error

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

I also tried using functions like sprintf (), but it doesnโ€™t look like the left pair more than the string concatenator. '.

So, is there a way to create class constants in terms of each other in something more than a trivial set case, such as foo2?

+43
php class const constants
May 7 '10 at 4:50
source share
6 answers

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

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

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

Although this is not part of the question, you need to know the limits of implementation. The following actions will not work, although this is static content (but can be controlled 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 more information, see: https://wiki.php.net/rfc/const_scalar_exprs and http://php.net/manual/en/language.oop5.constants.php

+15
Apr 18 '16 at 12:07 on
source share

The only way to define () an expression, and then use this 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 ask them to fix it.

+34
May 7 '10 at 9:40
source share

Always return to a reliable guide for such things.

Regarding constants :

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

So ... no will be the answer: D

+14
May 07 '10 at 5:02 a.m.
source share

For class constants, you cannot assign anything but a constant expression. Quoting PHP manual :

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

+2
May 7 '10 at 5:03 a.m.
source share

It may not be what you are looking for, but I came across this thread, so here is the solution I used for the problem I was facing (based on @ user187291 answer):

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

Seems to work great!

+1
Jun 06 '13 at 15:15
source share

no.

(I do not think there is)

0
May 07 '10 at 4:54
source share



All Articles