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?
$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
)
)
)
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'");
}
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"
}
}
}
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.
Since it says the undefined property is in stdClass
, this means that the object in question is not actually the User
class that you think it is.
This would generally imply that something went wrong with the creation of the object. So therefore the actual bug in your code that is leading to this error is earlier in the program than the line of code you've given us.
Look for where the object is being created. That's where the problem is likely to be.
I can't be of much more help than that without seeing the rest of the code, but hope that helps.
[EDIT]
The object that is throwing the error is $this->session->user
(this is the one you're trying to access the ->roles
property for).
As much as you want to say it's definitely a User
object, the fact is that PHP says otherwise. Do a var_dump($this->session->user)
immediately before the error and you should be able to see what I'm saying.
As for how come it isn't what you expect, I still can't give any better answer. Using a debugger like xDebug to trace through the program one line at a time might help.
The obvious explanation would be that the property is defined, but is protected. This means that it is only accessable to this and extended classes.
However, the error message does suggest some other error. The class is a User
class, but the error suggest it is a property of stdClass
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With