duangu1645 2009-11-18 12:11
浏览 21
已采纳

在类定义中不能使用sha1?

I'm working on a login class in PHP. I have the following line inside a class definition:

private $salty = sha1('salty');

...which is giving me the following error:

"Parse error: parse error, expecting ','' or';'' in C:\xampp\htdocs\test\includes\jaLogin.php on line 26"

I've tried using this line outside of the class definition and it works fine, it's only when I use it inside the class definition that I get a problem.

If I remove the word "private" I get a slightly different error:

Parse error: parse error, expecting `T_FUNCTION' in C:\xampp\htdocs\test\includes\jaLogin.php on line 26

I feel like I'm missing something obvious...

  • 写回答

3条回答 默认 最新

  • duanbeng8872 2009-11-18 12:13
    关注

    You can't use function-calls to initialize class member variables.

    class YourClass
    {
        private $salty;
    
        public function __construct()
        {
            $this->salty = sha1('salty');
        }
    }
    

    is the one way to initialize your variable.

    EDIT

    Even, e.g., a simple concatenation of two constant strings is not allowed (protected $_string = 'Hello ' . 'World!';). The evaluation of class properties happens at compile time, so the usage of constructs that depend on run-time information is illegal.

    [...] This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

    (Properties)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包