Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"PHP Notice: Undefined property" [closed]

Tags:

php

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.

like image 218
efpies Avatar asked Oct 07 '12 19:10

efpies


2 Answers

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.

like image 81
Spudley Avatar answered Oct 07 '22 01:10

Spudley


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.

like image 24
JvdBerg Avatar answered Oct 07 '22 03:10

JvdBerg