isset 无法确定属性是否存在 [英] isset not able to determine if property exists
本文介绍了isset 无法确定属性是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用 isset()
来确定此属性是否包含某些内容.我运行以下检查:
I tried to use isset()
to determine if this property contains something. I run the following checks:
首先,我确定 $property->property_address
是否包含某些内容:
First, I determine if $property->property_address
contains something:
var_dump($property->property_address);
// Outputs
// string(5) "hello"
现在,如果我在 isset()
中尝试:
Now if I try it in isset()
:
var_dump(isset($property->property_address));
// Outputs
// bool(false)
这是为什么?这就是为什么它不会在我的 if-else 上继续,因为 isset 返回 false.
Why is that? That is why it won'r proceed on my if-else, because isset returns false.
推荐答案
这是您重现这种确切行为的方式:
This is how you reproduce this exact behaviour:
<?php
class Shoop {
public $actual = 'hello';
public $mystery = array();
public function __set($name, $value) {
$this->mystery[$name] = $value;
}
public function __get($name) {
if(isset($this->mystery[$name])) {
return $this->mystery[$name];
} else { return null; }
}
}
$woop = new Shoop();
echo $woop->actual . "\n";
$woop->fake = 'da' . "\n";
echo $woop->fake;
var_dump(isset($woop->fake));
输出:
hello
da
bool(false)
然后你用:
public function __isset($name) {
return isset($this->mystery[$name]);
}
这篇关于isset 无法确定属性是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文