2010-07-25 29 views
2

爲什麼這會給出錯誤?有趣的錯誤用表達式初始化類變量

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

ROFLMAO! 「期待」......這就是她*說的...... XD – deceze 2010-07-25 02:31:48

回答

7

md5(time())是一個表達式。

字段初始化不允許使用表達式,只能使用文字。

相反,你可以這樣做:

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

    public function __construct() 
    { 
     $this->sid = md5(time()); 
    } 
} 
+2

+5票,但沒有人注意到他的答案中的錯誤。 2'= ='在你的函數中。無論如何,我會解決它。 – Starx 2010-07-25 02:29:54

+0

@Starx - 哎呀,謝謝! – 2010-07-25 03:15:12