不能为类中的静态变量赋值



我正在尝试为一个类实现单例模式。我有一个静态变量$classInstance,在分配类对象时收到错误"解析错误:语法错误,意外的'='"。

/**
* Hold instance of class.
*/
private static $classInstance = null;
/**
* Create the client connection.
*/
public static function createClient() {
if (self::classInstance === null) {
self::classInstance = new self(); // Getting error on this line.
}
return self::classInstance;
}

错误是因为在使用 self 关键字访问类的静态属性时缺少"$"。

/**
* Create the client connection.
*/
public static function createClient() {
if (self::$classInstance === null) {
self::$classInstance = new self(); // Note the $ sign.
}
return self::$classInstance;
}

最新更新