我可以在 PHP 中使用字符串连接来定义类 CONST 吗?

发布于 2024-08-31 11:00:12 字数 505 浏览 6 评论 0原文

我知道您可以使用字符串连接来创建彼此之间的全局常量:

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

将打印“foobar”。

但是,我在尝试使用类常量执行相同操作时遇到错误。

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

foo2 的定义没有问题,但声明 const bar 会出错

解析错误:语法错误,意外的“.”,期望“,”或“;”

我也尝试过使用像 sprintf() 这样的函数,但它不喜欢左括号,也不喜欢字符串连接符“.”。

那么,除了像 foo2 这样的简单集合案例之外,还有什么方法可以相互创建类常量呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻的味道 2024-09-07 11:00:12

唯一的方法是定义()一个表达式,然后在类中使用该常量

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

class Foo
{
    const blah = foobar;
}

echo Foo::blah;

另一个选择是访问 bugs.php.net 并恳请他们解决这个问题。

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.

苏佲洛 2024-09-07 11:00:12

恕我直言,这个问题值得 PHP 5.6+ 的答案,感谢 @jammin comment

从 PHP 5.6 开始,您可以为常量定义静态标量表达式:

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

虽然它不是问题的一部分,但应该是意识到实施的局限性。以下内容不起作用,尽管它是静态内容(但可能在运行时被操纵):

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 ';'

有关更多信息,请查看: https://wiki.php.net/rfc/const_scalar_exprshttp://php.net/manual/en/language.oop5.constants.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

时常饿 2024-09-07 11:00:12

对于此类内容,请始终参考可靠的手册。

关于常量

该值必须是常量
表达式,而不是(例如)a
变量、属性、结果
数学运算,或函数
打电话。

所以......“不”就是答案:D

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

简单爱 2024-09-07 11:00:12

对于常量,除了常量表达式之外,您不能分配任何内容。引用 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. "

喜爱纠缠 2024-09-07 11:00:12

这可能不是您直接寻找的,但我遇到了这个线程,所以这是我用于解决我遇到的问题的解决方案(基于@user187291的答案):

define('app_path', __DIR__ . '/../../');
const APPLICATION_PATH = app_path;
.
.
.
require_once(APPLICATION_PATH . "some_directory/some_file.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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文