Starx
Starx

Reputation: 79039

Funny Error initialiazing a class variable with expression

Why is this giving error?

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

Upvotes: 2

Views: 209

Answers (1)

Amy B
Amy B

Reputation: 17977

md5(time()) is an expression.

Field initializations are not allowed to use expressions, only literals.

Instead, you could do:

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

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

Upvotes: 7

Related Questions