php常量数学运算

标签 php constants

目前我正在学习php。我很困惑这是我的 php 代码

class OBJECT_ENUM
{
    const USER = 10;
    const POST = 30;
    const SECURE_REQUEST = 40;
}

class OPERATION_ENUM
{
    const INSERT_USER = OBJECT_ENUM::USER + 1; // <- here it gives an error
    const SEND_MAIL = OBJECT_ENUM::USER + 2;

    const LIKE_POST = OBJECT_ENUM::POST + 1;
    const INSERT_POST = OBJECT_ENUM::POST + 2;

    const ENCRYPT = OBJECT_ENUM::SECURE_REQUEST + 1;
}

error message: 
Parse error: syntax error, unexpected '+', expecting ',' or ';' in /var/www/workspace/6thAssignment/include/tempCall.php on line 15

我只是不明白为什么会发生这个错误。?有人能给我解释一下吗??

提前谢谢

最佳答案

原始答案:

正如您在 http://www.php.net/manual/en/language.oop5.constants.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.

更新:

从 PHP 版本 5.6 now it is possible to use expressions为常数。

关于php常量数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510501/

相关文章:

c++ - const 成员函数/方法的用处

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

php - Fontawesome 字体在 Assets :install and assetic:dump 后失败

php - 有没有办法更改 Zend Framework 中的路由变量分隔符?

php - DisMax 解析用户查询和 q 或 fq 过滤结果

php - 在 OpenBSD 的 Apache 服务器上运行 xinc

c++ - 如何定义指向 const 对象的可变指针?

c++ - 当我希望调用基类构造函数时,如何在派生类中初始化成员 const 引用?

c - 使用 '.' token 定义常量

php - 如果选择了确定的选项,如何启用字段?