php 警告:尝试分配非对象的属性,但对象存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32855705/
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
Warning: Attempt to assign property of non-object, but the Object exists
提问by marcusds
This is my class:
这是我的课:
class Network {
protected $value = null;
protected $properties = array();
public $type = null;
function __construct($value = null, $type = null) {
$this->value = $value;
$this->type = $type;
}
public function __set($name, $value) {
if(isset($this->{$name})) {
$this->{$name} = $value;
} else {
$this->properties[$name] = new self($value, null);
}
}
public function __get($name) {
return $this->properties[$name]->value;
}
public function __toString() {
return $this->value;
}
}
This is what I am trying to do:
这就是我想要做的:
$networks = new \stdClass();
$networks->test= new \Orange_Post\Network('Test');
$networks->test->enabled = true;
$networks->test->enabled->type = 'boolean';
But I get the error:
但我收到错误:
Warning:Attempt to assign property of non-object on the last line, $networks->test->enabled->type = 'boolean';
警告:尝试在最后一行分配非对象的属性,$networks->test->enabled->type = 'boolean';
This is my first attempt at branching out and doing something like this, and I just can't figure out what I am doing incorrectly.
这是我第一次尝试扩展并做这样的事情,我只是无法弄清楚我做错了什么。
回答by Rizier123
So what is going on here?
那么这里发生了什么?
$networks = new \stdClass();
$networks->test= new \Orange_Post\Network('Test');
$networks->test->enabled = true;
$networks->test->enabled->type = 'boolean';
↑ ↑ ↑ ↑ Tries to access a property of a value ('TRUE')
| | | 'enabled' as property never exists
| | Is a property with an instance of '\Orange_Post\Network'
| Is an instance of '\stdClass'
First off, when you try to assign true
to the property enabled
. Then __set()
gets called, since the property doesn't exists.
首先,当您尝试分配true
给属性时enabled
。然后__set()
被调用,因为该属性不存在。
public function __set($name, $value) {
if(isset($this->{$name})) {
$this->{$name} = $value;
} else {
$this->properties[$name] = new self($value, null);
}
}
In that magic method you assign a new instace of the class itself to the property array properties
and use the name (enabled
) as index.
在那个神奇的方法中,您将类本身的一个新实例分配给属性数组,properties
并使用名称 ( enabled
) 作为索引。
$networks->test->enabled->type = 'boolean';
After this you try to set a property type
to the property enabled
. Here __get
gets invoked.
在此之后,您尝试type
为该属性设置一个属性enabled
。这里__get
被调用。
public function __get($name) {
return $this->properties[$name]->value;
}
So now you simply return the array value of properties
and return the property value
of it. And the property value
is not an object. Just remove the ->value
part and your code will return the object.
所以现在你只需返回数组的值properties
并返回value
它的属性。并且该属性value
不是对象。只需删除该->value
部分,您的代码就会返回该对象。
In other words your last line:
换句话说,你的最后一行:
$networks->test->enabled->type = 'boolean';
Becomes:
变成:
$networks->test->properties["enabled"]->value->type = 'boolean';
↑ ↑ This is causing the problem
| Holds an object | Holds 'true' as property value
回答by Mark Schaschke
You are assigning the enabled
property a value of true
, however the next line is trying to treat that property as an object (it's not, it's a boolean value). If you really need to have a value and type for the enabled
property, try this instead:
您正在为该enabled
属性分配一个值true
,但是下一行试图将该属性视为一个对象(不是,它是一个布尔值)。如果您确实需要为该enabled
属性设置值和类型,请尝试以下操作:
$networks->test->enabled = new \stdClass();
$networks->test->enabled->value = true;
$networks->test->enabled->type = 'boolean';
Hope this helps!
希望这可以帮助!