Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有对象:
class Obj { public $foo; public $bar; } $obj = new Obj(); print_R($obj);
输出:
Obj Object ( [foo] => [bar] => )
但
var_dump(isset($obj->foo));
输出bool(false)。
bool(false)
如何检查对象中是否设置了变量?
因为
$foo = null; var_dump(isset($foo)); // false
也许你想用property_exists或ReflectionClass::hasProperty检查它
您应该同时使用isset和property_exists:
isset
property_exists
if (property_exists($obj,$foo) && isset($obj->$foo)) { // use $obj->foo }
一个属性可能存在,没有任何设置,无法按预期访问。