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

Php 在yii上不在对象上下文中时使用$this,php,yii,Php,Yii,我有上面的型号。我在使用指定变量时遇到问题。 上面代码中的错误是 class ModelFile extends CoreModel{ public $var; public $var2; public static function getId() { $this->var = '123'; } } 知道这个问题吗?您不能在静态方法中访问$this,因为它们不属于任何实例,而是属于整个类。在您的情况下,只需从getId函数中删除static关键字。removestatic。

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

class ModelFile extends CoreModel{

public $var;
public $var2;

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

知道这个问题吗?

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

remove
static
Fatal error: Using $this when not in object context