How to create a constant variable of a static class from another constant variable?

I have a constant called PREFIX defined in constants.php. In the Foo class, I would like to create a static class constant with PREFIX as the prefix. But I get a syntax error in this line of const definition.

require_once 'constants.php'; class Foo { const FOO_CONST = PREFIX . 'bar'; public function __construct() { } } 
+4
source share
1 answer

In PHP, a const should be a value, not an expression. Therefore const FOO_CONST = 'foo' . 'bar'; const FOO_CONST = 'foo' . 'bar'; will not work either.

You must use define or a member of the class that is initialized in the constructor instead of const . Initializing a class member outside the class method with an expression does not work either.

+4
source

Source: https://habr.com/ru/post/1400291/


All Articles