静态类属性在编译时被初始化.在初始化静态类属性时,不能使用常量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';
}
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对表达式的支持有限:
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.