I’ve made a class:
class MyClass {
function __get($name) {
$this->{$name} = $_SESSION[$name];
}
public function isNull(){
return $this->MyVar === null;
}
}
var_dump($obj->isNull()) //$obj is instance of MyClass always outputs bool(true) doesn’t matter $_SESSION['MyVar'] is set or not. But if I add $this->MyVar; before return $this->MyVar === null; works as expected.
Well it seems that __get() is executed (because $MyVar is not set so it’s inaccessible) but property stays NULL until second usage.
Does it happen because it’s not set at all? How can I avoid using extra line ($this->MyVar;)?
__get()must return the value. Change it to: