isset或property_exists都不适合我。
如果属性存在但是为NULL,则isset返回false。
如果属性是对象的类定义的一部分,则property_exists返回true,即使它已被取消设置也是如此。
我结束了:
$exists = array_key_exists($property, get_object_vars($obj));
例:
class Foo {
public $bar;
function __construct() {
$property = 'bar';
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
unset($this->$property);
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // FALSE
$this->$property = 'baz';
isset($this->$property); // TRUE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
}
}