在类常量 PHP 中组合位标志

发布于 2024-12-29 02:52:40 字数 1902 浏览 0 评论 0原文

可能的重复:
未解析基本语法的解决方法

我试图允许开发人员指定任意位的组合来指定他们想要包含在响应中的数据片段。

    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 技术交流群。

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

发布评论

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

评论(2

樱花坊 2025-01-05 02:52:40

你可以自己计算一下。由于这些是位标志,因此存在一种模式。

class ClassName {
        const BUILD_DATE_RFC = 1; 
        const BUILD_DATE_SQL = 2;
        const BUILD_DATE_SQLTIME = 4;
        const BUILD_DATE_UNIX = 8;
        const BUILD_DATE_ALL = 15; // 15 = 1|2|4|8;
       // ....
}

You can calculate it yourself. As these are bit flags there is a pattern.

class ClassName {
        const BUILD_DATE_RFC = 1; 
        const BUILD_DATE_SQL = 2;
        const BUILD_DATE_SQLTIME = 4;
        const BUILD_DATE_UNIX = 8;
        const BUILD_DATE_ALL = 15; // 15 = 1|2|4|8;
       // ....
}
掀纱窥君容 2025-01-05 02:52:40

引用手册:

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

使用|运算符是运算的结果,因此不允许

QUoting from the 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.

use of the | operator is the result of an operation, therefore not pemitted

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