Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange parse error with static concatenated string variable [closed]

I'm getting this error:

Parse error: syntax error, unexpected '.', expecting ',' or ';' in /var/(...)/config.php on line 5

With this (simplified) code:

<?php

class Config
{
   public static $somevar = "Date: " . date('Y');
}

?>

I thought this was valid php, but I guess not... what am I doing wrong here? Thanks!

like image 351
Herman Cordes Avatar asked Nov 24 '10 13:11

Herman Cordes


1 Answers

According to the PHP docs:

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

Try writing

Config::$somevar = "Date: " . date('Y');

after the class definition.

like image 159
cdhowie Avatar answered Oct 27 '22 00:10

cdhowie