2010-11-24 124 views
5

我得到这个错误:奇怪的语法错误

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

有了这个(简化)代码:

<?php 

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

?> 

我认为这是有效的PHP,但我想不会...我在这里做错了什么?谢谢!

回答

5

根据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.

尝试类定义后写

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

+1

谢谢,不知道! – 2010-11-24 13:08:35

1

Manual

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.

2

无操作或功能是允许属性初始化,因为在解析时,这是评估。