这件事让我烦恼不已。我得到Parse错误:语法错误,意外'。',期待','或';'在这一行
public static $user_table = TABLE_PREFIX . 'users';
TABLE_PREFIX是由define function
创建的常量答案 0 :(得分:21)
静态类属性在编译时初始化。在初始化静态类属性时,不能使用常量TABLE_PREFIX
与字符串文字连接,因为在运行时才知道常量的值。而是在构造函数中初始化它:
public static $user_table;
// Initialize it in the constructor
public function __construct() {
self::$user_table = TABLE_PREFIX . 'users';
}
// If you only plan to use it in static context rather than instance context
// (won't call a constructor) initialize it in a static function instead
public static function init() {
self::$user_table = TABLE_PREFIX . 'users';
}
http://us2.php.net/manual/en/language.oop5.static.php
与任何其他PHP静态变量一样,静态属性只能使用文字或常量初始化;表达式是不允许的。因此,虽然您可以将静态属性初始化为整数或数组(例如),但您可能不会将其初始化为另一个变量,函数返回值或对象。
PHP 5.6对表达式的支持有限:
在PHP 5.6及更高版本中,相同的规则适用于const表达式:只要可以在编译时对它们进行求值,就可以使用一些有限的表达式。
答案 1 :(得分:8)
点是字符串连接运算符。它是一个运行时函数,因此不能用于声明静态(parsetime)值。