ReflectionClass :: GetProperties中
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
使用get_object_vars将返回newProp,但不会返回受保护和私有成员.
因此,根据您的需要,可能需要反射和get_object_vars的组合.