无法在yii模型函数中获取变量的值

时间:2018-06-01 02:39:30

标签: php yii model

无法获取函数中变量的值。当我打印$this->var时,它让我空洞。我在这些步骤中缺少什么?

class ModelFile extends CoreModel{

 public $var;
 public $var2;

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

 public function getId(){
  $this->setId();
  var_dump($this->var); //Empty
 }

}

1 个答案:

答案 0 :(得分:0)

试试这个

class ModelFile extends CoreModel{

    public $var;
    public $var2;

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

    public function getId(){
      $this->setId();
      var_dump($this->var); //Empty
    }
 }
 **create an object of the above class and call the function getId() then it 
 will execute and print the var**

 $model = new ModelFile();
 $model->getId();