“PHP 通知:未定义的属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12772083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:12:03  来源:igfitidea点击:

"PHP Notice: Undefined property"

php

提问by efpies

I'm getting this strange error. You'll say: "Why strange? You just don't have such property". No. Problem is there areproperty.

我收到这个奇怪的错误。你会说:“为什么奇怪?你只是没有这样的财产”。首要问题是存在财产。

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 iscalled 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

UPD1

$this->user->sessionis an Userobject

$this->user->session是一个User对象

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

UPD2

All properties are accessed via magic __get()

所有属性都通过魔法访问 __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

UPD3

var_dump($this->session->user)

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->idin place where $this->session->useris not created yet. So null->idactually was (new stdClass())->id. Well, thank you, PHP.

在一个地方,我不小心写到了尚未创建的$this->session->user->id = $user->id地方$this->session->user。所以null->id实际上是(new stdClass())->id。嗯,谢谢你,PHP

采纳答案by Spudley

Since it says the undefined property is in stdClass, this means that the object in question is not actually the Userclass that you think it is.

由于它说 undefined 属性是 in stdClass,这意味着所讨论的对象实际上并不是User您认为的类。

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 ->rolesproperty for).

引发错误的对象是$this->session->user(这是您尝试访问其->roles属性的对象)。

As much as you want to say it's definitely a Userobject, 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.

尽管您想说它绝对是一个User对象,但事实是 PHP 另有说法。做一个var_dump($this->session->user)立即错误之前,你应该能够看到我在说什么。

As for how come it isn't what you expect, I still can't give any better answer. Using a debugger like xDebugto trace through the program one line at a time might help.

至于为什么不是你所期望的,我仍然无法给出更好的答案。使用像xDebug这样的调试器一次一行地跟踪程序可能会有所帮助。

回答by JvdBerg

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 Userclass, but the error suggest it is a property of stdClass.

但是,错误消息确实暗示了一些其他错误。该类是一个User类,但错误表明它是stdClass.