这件事让我烦恼不已.我得到Parse错误:语法错误,意外'.',期待','或';' 在这条线上
public static $user_table = TABLE_PREFIX . 'users';
Run Code Online (Sandbox Code Playgroud)
TABLE_PREFIX是define函数创建的常量
Mic*_*ski 22
静态类属性在编译时初始化.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';
}
Run Code Online (Sandbox Code Playgroud)
http://us2.php.net/manual/en/language.oop5.static.php
与任何其他PHP静态变量一样,静态属性只能使用文字或常量初始化; 表达式是不允许的.因此,虽然您可以将静态属性初始化为整数或数组(例如),但您可能不会将其初始化为另一个变量,函数返回值或对象.
PHP 5.6为表达式提供了有限的支持:
在PHP 5.6及更高版本中,相同的规则适用于const表达式:一些有限的表达式是可能的,只要它们可以在编译时进行评估.
| 归档时间: |
|
| 查看次数: |
24776 次 |
| 最近记录: |