1

I'm new to OOP and Yii2. I have a function in Model:

public function getDatRev() {
    if ($this->rev) {
        return $this->rev;
    } else {
        return $this->datum;
    }
}

in the View until now I have used it like this:

$model->datRev;

and it would return the correct value. Now I don't know what has changed, maybe I was also updated the framework, but the old construct doesn't work anymore, and in order to make it work I have to change it to:

$model->getDatRev();

Can you please explain to me why that is?

Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
user2511599
  • 796
  • 1
  • 13
  • 38

2 Answers2

1

When you try get property the Yii2 calls magic method __get (). Return value is depend from implementation of this method in parent class. Yii2 can check if this property exist in some container, or if exist getter of this property. In your case seems like you don't call parent's method __get(). This may have happened because you override __get() method or initialized this property.

Timur
  • 488
  • 4
  • 14
1

Your class needs to extend yii\base\Object (directly or not) in order to use short property syntax ($model->abc instead of $model->getAbc()). Magic method __get() @Timur mentioned is defined there and further extended in yii\base\Component class.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • I have generated my models with giiant and I definitely haven't changed such base things. If it was like this before (because it was working), than it's also like this now. Probably I have changed something, but I have no clue what could result something like this. This is crazy... – user2511599 Jun 02 '17 at 20:44