有趣的错误用表达式初始化类变量

Sta*_*arx 2 php oop

为什么这会给出错误?

class content {
    protected $id,$title,$content,$image,$imagedirectory,$page;
    protected $sid = md5(time()); //In this line : parse error, expecting `','' or `';''
}
Run Code Online (Sandbox Code Playgroud)

Amy*_*y B 7

md5(time()) 是一种表达.

字段初始化不允许使用表达式,只允许使用文字.

相反,你可以这样做:

class content {
    protected $id, $title, $content, $image, $imagedirectory, $page, $sid;

    public function __construct()
    {
        $this->sid = md5(time());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +5票,但没有人注意到他的答案中的错误.2` = =`在你的函数中.无论如何我会解决它. (2认同)