解析错误:语法错误,意外的“.",预期为“,"或“;" [英] Parse error: syntax error, unexpected '.', expecting ',' or ';'
问题描述
这件事困扰着我很多.我收到解析错误:语法错误,意外的.",期望为,"或;"在这一行
This thing is bugging me a lot. I'm getting Parse error: syntax error, unexpected '.', expecting ',' or ';' at this line
public static $user_table = TABLE_PREFIX . 'users';
TABLE_PREFIX是由定义函数创建的常量
TABLE_PREFIX is a constant created by define function
推荐答案
静态类属性在编译时初始化.初始化静态类属性时,不能使用常量TABLE_PREFIX
与字符串文字连接,因为在运行时才知道常量的值.而是在构造函数中对其进行初始化:
Static class properties are initialized at compile time. You cannot use a constant TABLE_PREFIX
to concatenate with a string literal when initializing a static class property, since the constant's value is not known until runtime. Instead, initialize it in the constructor:
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/zh/language.oop5.static.php
与其他任何PHP静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式.因此,尽管您可以将静态属性初始化为整数或数组(例如),但不能将其初始化为另一个变量,函数返回值或对象.
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.
针对PHP> = 5.6的更新
PHP 5.6带来了对表达式的有限支持:
Update for PHP >= 5.6
PHP 5.6 brought limited support for expressions:
在PHP 5.6和更高版本中,相同的规则与const表达式适用:只要可以在编译时对其进行求值,某些有限的表达式也是可能的.
In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.
这篇关于解析错误:语法错误,意外的“.",预期为“,"或“;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!