"PHP Note: Undefined property"

I get this strange error. You will say: "Why is it strange? You do not have such property." No. The problem is that there is a property.

There I get an error message.

// PHP Notice: Undefined property: stdClass::$roles in $canWrite = $this->session->isLoggedIn() ? $this->page->canWrite($this->session->user->roles) : false; 

This is a class.

 class User { protected $roles; function getRoles() { if (!$this->roles) { // Get them! } return $this->roles; } } 

So this method is called when I try to access a property on this line. Everything is working fine, but I do not want to increase my error log. What's happening?

UPD1

$this->user->session - 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 through 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 , where $this->session->user has not been created yet. So null->id really was (new stdClass())->id . Ok, thanks, PHP.

+4
source share
2 answers

Since it says that the undefined property is in stdClass , this means that the object in question is not really the User class that you think so.

This usually means that something went wrong with the creation of the object. Thus, the actual error in the code that leads to this error is earlier in the program than the line of code that you gave us.

Find where the object is created. That the problem is likely to be.

I can't get more help than without the rest of the code, but hope this helps.

[EDIT]

The object that throws the error is $this->session->user (this is the one you are trying to access the property ->roles ).

As far as you want to say that it is definitely a User object, the fact is that PHP says otherwise. Do var_dump($this->session->user) immediately before the error and you can see what I'm saying.

As for why this is not what you expect, I still cannot give a better answer. Using a debugger such as xDebug to track a program can help one line at a time.

+5
source

The obvious explanation is that the property is defined, but protected. This means that it is available only for this and extended classes.

However, the error message suggests another error. The class is a User class, but the error assumes that this is a stdClass property.

+2
source

Source: https://habr.com/ru/post/1438290/


All Articles