如何检查
PHP对象是否为空(即没有属性)?根据doc,内置的empty()不适用于对象:
5.0.0 Objects with no properties are no longer considered empty.
ReflectionClass :: GetProperties中
原文链接:https://www.f2er.com/php/135874.htmlhttp://www.php.net/manual/en/reflectionclass.getproperties.php
class A { public $p1 = 1; protected $p2 = 2; private $p3 = 3; } $a = new A(); $a->newProp = '1'; $ref = new ReflectionClass($a); $props = $ref->getProperties(); // now you can use $props with empty echo empty($props); print_r($props); /* output: Array ( [0] => ReflectionProperty Object ( [name] => p1 [class] => A ) [1] => ReflectionProperty Object ( [name] => p2 [class] => A ) [2] => ReflectionProperty Object ( [name] => p3 [class] => A ) ) */
请注意,newProp不会在列表中返回.
get_object_vars
http://php.net/manual/en/function.get-object-vars.php
使用get_object_vars将返回newProp,但不会返回受保护和私有成员.
因此,根据您的需要,可能需要反射和get_object_vars的组合.