问题
I tried to use isset()
to determine if this property contains something. I run the following checks:
First, I determine if $property->property_address
contains something:
var_dump($property->property_address);
// Outputs
// string(5) "hello"
Now if I try it in isset()
:
var_dump(isset($property->property_address));
// Outputs
// bool(false)
Why is that? That is why it won'r proceed on my if-else, because isset returns false.
回答1:
Try doing both var_dumps at the same call and check the output
var_dump($property->property_address);
var_dump(isset($property->property_address));
回答2:
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));
Outputs:
hello
da
bool(false)
And you address it with:
public function __isset($name) {
return isset($this->mystery[$name]);
}
Documentation
来源:https://stackoverflow.com/questions/20059666/isset-not-able-to-determine-if-property-exists