I'm getting this strange error. You'll say: "Why strange? You just don't have such property". No. Problem is there are property.
There I'm getting an error.
// PHP Notice: Undefined property: stdClass::$roles in
$canWrite = $this->session->isLoggedIn() ? $this->page->canWrite($this->session->user->roles) : false;
This is the class.
class User {
protected $roles;
function getRoles() {
if (!$this->roles)
{
// Get them!
}
return $this->roles;
}
}
So this method is called when I'm trying to access property in this line. Everything works fine but I don't want to increase my error log. What's happening?
UPD1
$this->user->session
is an User
object
function getUser() {
if (!$this->user) {
$u = new User();
// Logic
$this->user = $u;
}
return $this->user;
}
User Object
(
[roleId:protected] => 1
[roles:protected] => Array
(
[root] => Role Object
(
[id:protected] => 1
[hrefname:protected] => root
)
)
)
UPD2
All properties are accessed via magic __get()
public function __get($var) {
if ($this->__isset($var)) {
$method = 'get'.ucfirst($var);
if (method_exists($this, $method)) {
return $this->$method();
} else {
return $this->$var;
}
}
throw new Exception("Unrecognized attribute '$name'");
}
UPD3
var_dump($this->session->user)
object(User)#370 (30) {
["roles":protected]=>
array(1) {
["root"]=>
object(Role)#372 (2) {
["id":protected]=>
string(1) "1"
["hrefname":protected]=>
string(4) "root"
}
}
}
Explanation
In one place I accidentally wrote $this->session->user->id = $user->id
in place where $this->session->user
is not created yet. So null->id
actually was (new stdClass())->id
. Well, thank you, PHP.