gpt4 book ai didi

php - 解析错误 : syntax error, unexpected '.' expecting ',' or ';'

转载 作者:IT王子 更新时间:2023-10-29 00:11:53 25 4
gpt4 key购买 nike

这件事一直困扰着我。我收到 Parse error: syntax error, unexpected '.', expecting ',' or ';'在这一行

public static $user_table = TABLE_PREFIX . 'users';

TABLE_PREFIX 是由定义函数创建的常量

最佳答案

静态类属性在编译时初始化。在初始化静态类属性时,不能使用常量 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

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.

关于php - 解析错误 : syntax error, unexpected '.' expecting ',' or ';',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com