我可以使用字符串连接在 PHP 中定义类 CONST 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2786279/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use string concatenation to define a class CONST in PHP?
提问by selfsimilar
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'.
将打印“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
foo2 的定义没有问题,但声明 const bar 会出错
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 '.'.
我也尝试过使用 sprintf() 之类的函数,但它不喜欢左括号而不是字符串连接符 '.'。
So is there any way to create class constants in terms of each other in anything more than a trivial set case like foo2?
那么,除了像 foo2 这样的简单集合之外,还有什么方法可以相互创建类常量吗?
采纳答案by Trendfischer
Imho, this question deserves an answer for PHP 5.6+, thanks to @jammin comment
恕我直言,这个问题值得PHP 5.6+回答,感谢@jammin评论
Since PHP 5.6 you are allowed to define a static scalar expressions for a constant:
从 PHP 5.6 开始,您可以为常量定义静态标量表达式:
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_exprsand http://php.net/manual/en/language.oop5.constants.php
有关更多信息,请查看:https: //wiki.php.net/rfc/const_scalar_exprs和http://php.net/manual/en/language.oop5.constants.php
回答by user187291
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.
另一种选择是去 bugs.php.net 并请他们解决这个问题。
回答by Peter Bailey
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
所以......“不”将是答案:D
回答by Dumb Guy
For classconstants, you can't assign anything other than a constant expression. Quoting the PHP manual:
对于类常量,除了常量表达式之外,您不能分配任何东西。引用PHP 手册:
"The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. "
“该值必须是常量表达式,而不是(例如)变量、属性、数学运算的结果或函数调用。”
回答by MuffinTheMan
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):
这可能不是您正在寻找的直接内容,但我遇到了这个线程,所以这是我用于解决我遇到的问题的解决方案(基于@user187291 的回答):
define('app_path', __DIR__ . '/../../');
const APPLICATION_PATH = app_path;
.
.
.
require_once(APPLICATION_PATH . "some_directory/some_file.php");
.
.
.
Seems to work great!
看起来效果很好!

