在类常量 PHP 中组合位标志
可能的重复:
未解析基本语法的解决方法
我试图允许开发人员指定任意位的组合来指定他们想要包含在响应中的数据片段。
class ClassName {
const BUILD_DATE_RFC = 1;
const BUILD_DATE_SQL = 2;
const BUILD_DATE_SQLTIME = 4;
const BUILD_DATE_UNIX = 8;
// ....
}
这在某种意义上是有效的,当我像这样实例化该类时:
$whatever = new ClassName(BUILD_DATE_RFC|BUILD_DATE_SQL);
将执行此逻辑:
if (self::BUILD_DATE_RFC & $this->metaBits) {
$dateMeta['RFC'] = date('r');
}
if (self::BUILD_DATE_SQL & $this->metaBits) {
$dateMeta['SQL'] = date('Y-m-d');
}
if (self::BUILD_DATE_SQLTIME & $this->metaBits) {
$dateMeta['SQL_time'] = date('Y-m-d H:i:s');
}
所有这些都工作得很好,除了我想定义“快捷方式位”之类的东西,例如 BUILD_DATE_ALL ,这将是所有的总和的值DATE 相关位,因此他们只需指定该快捷方式常量,而不是单独指定每个常量。
我尝试了这个,但它引发了一个错误:
const BUILD_DATE_ALL = (self::BUILD_DATE_RFC|self::BUILD_DATE_SQL|self::BUILD_DATE_SQLTIME|self::BUILD_DATE_UNIX);
我还尝试了不同的方法/语法:
const BUILD_REQUEST_ALL = self::BUILD_IP |
self::BUILD_USERAGENT |
self::BUILD_REFERER;
以及我尝试过的另一种方法:
const BUILD_DEFAULT = self::BUILD_DATE_ALL|self::BUILD_REQUEST_ALL^self::BUILD_REFERER^self::BUILD_USERAGENT;
我得到的错误是:
ErrorException:语法错误,意外的“(”
,其他方法得到的错误是:
ErrorException:语法错误,意外的“|”,需要“,”或“;”
看起来 PHP 不想在常量定义中计算太多,而只想要单个值而不是派生值。我假设这是基于这样一个事实:它不需要括号,也不需要 |进行进一步的计算。此外,我尝试使用“-”而不是 |只是为了测试我的理论..是的,它也抱怨 + 出乎意料。
我该如何解决这个问题,以便我可以定义一个“快捷方式位”,它是其他已定义常量范围的总和。
Possible Duplicate:
Workaround for basic syntax not being parsed
I am trying to allow developers to specify any combination of bits to specify which pieces of data they want included in a response.
class ClassName {
const BUILD_DATE_RFC = 1;
const BUILD_DATE_SQL = 2;
const BUILD_DATE_SQLTIME = 4;
const BUILD_DATE_UNIX = 8;
// ....
}
This works in the sense that when I instantiate the class like this:
$whatever = new ClassName(BUILD_DATE_RFC|BUILD_DATE_SQL);
This logic would be executed:
if (self::BUILD_DATE_RFC & $this->metaBits) {
$dateMeta['RFC'] = date('r');
}
if (self::BUILD_DATE_SQL & $this->metaBits) {
$dateMeta['SQL'] = date('Y-m-d');
}
if (self::BUILD_DATE_SQLTIME & $this->metaBits) {
$dateMeta['SQL_time'] = date('Y-m-d H:i:s');
}
All of this works beautifully, except I'd like to define 'shortcut bits' something like BUILD_DATE_ALL which would be the value of the sum of all the DATE related bits so they only have to specify that shortcut constant rather than each one individually.
I tried this but it throws an error:
const BUILD_DATE_ALL = (self::BUILD_DATE_RFC|self::BUILD_DATE_SQL|self::BUILD_DATE_SQLTIME|self::BUILD_DATE_UNIX);
I also tried different approaches/syntaxes:
const BUILD_REQUEST_ALL = self::BUILD_IP |
self::BUILD_USERAGENT |
self::BUILD_REFERER;
and another approach I tried:
const BUILD_DEFAULT = self::BUILD_DATE_ALL|self::BUILD_REQUEST_ALL^self::BUILD_REFERER^self::BUILD_USERAGENT;
The error I get is:
ErrorException: syntax error, unexpected '('
and the error I get for the other approaches is:
ErrorException: syntax error, unexpected '|', expecting ',' or ';'
It looks like PHP doesn't want to calculate too much in a constant definition and just wants a single value rather than derived value. I'm assuming this based off the fact that it doesn't want parenthesis nor does it want the | to do further calculations. Additionally, I tried using '-' instead of | just to test my theory.. and yes, it complained about the + being unexpected too.
How do I go about fixing the problem so I can define a 'shortcut bit' which is a sum of a range of the other already-defined constants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以自己计算一下。由于这些是位标志,因此存在一种模式。
You can calculate it yourself. As these are bit flags there is a pattern.
引用手册:
使用|运算符是运算的结果,因此不允许
QUoting from the manual:
use of the | operator is the result of an operation, therefore not pemitted