isset not able to determine if property exists

旧时模样 提交于 2021-01-29 15:13:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!