如何使用 PHP 连接常量和变量并将其存储在类常量中?
class My_class { const STATUS_ERROR = 0; const STATUS_OK = 1; const DB_TABLE = TABLE_PREFIX . 'class_table'; }
这两个状态常量工作正常,并且可以在类方法中作为 self::STATUS_ERROR
和 self::STATUS_OK
进行访问。
问题是当我尝试定义第三个常量时如何阻止抛出以下错误。
解析错误:语法错误、意外的“.”、期望的“,”或“;”在/home/sub/sub/directory/script.php
class My_class { const STATUS_ERROR = 0; const STATUS_OK = 1; const DB_TABLE = TABLE_PREFIX . 'class_table'; }
The two status consts work fine and can be accessed within class methods as self::STATUS_ERROR
and self::STATUS_OK
just fine.
The issue is one of how to stop the following error being thrown when I try to define the third constant.
Parse error: syntax error, unexpected '.', expecting ',' or ';' in /home/sub/sub/directory/script.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不知道。常数是常数。你不能在其中存储任何东西。
不过,您可以使用静态属性。
您无法在声明中执行此操作,因此您可能更喜欢静态方法。
You don't. Constants are constant. You can't store anything in them.
You can use a static property though.
You can't do it within the declaration, so you might prefer a static method instead.
const 必须用常量值定义,它们不能是表达式
http://www.phpbuilder.com/manual/en/language.oop5.constants.php
a const must be defined with a constant value, they can't be the result of an expression
http://www.phpbuilder.com/manual/en/language.oop5.constants.php