当不在 yii 的对象上下文中时使用 $this

Using $this when not in object context on yii

class ModelFile extends CoreModel{

public $var;
public $var2;

public static function getId() {
   $this->var = '123';
}
}

我有以上型号。我在使用分配变量时遇到问题。 我在上面的代码中得到的错误是

Fatal error: Using $this when not in object context

知道这个问题吗?

您不能在静态方法中访问 $this,因为它们不属于任何实例,而是属于整个 class。在您的情况下,只需删除 static 关键字。