$obj = (object)array('a', 'b', 'c');
$refl = new \ReflectionObject($obj);
$props = $refl->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED);
foreach($props as $prop)
print $prop->name;
打印一些奇怪的名称,如line
,file
和line
(而不是1,2,3)。为什么?我意识到名称是无效的,因为它们是数字,但为什么我会得到这些随机字符串?奇怪行为
get_object_vars($obj)
说明不了什么,而实际上print_r((array)$obj)
正确打印值。
如果反射无法显示数字属性,有什么办法可以让它忽略它们吗?
它也发生在许多SPL interators(如ArrayObject
S,SplFixedArray
S,SplHeap
S)。显然,这种行为只存在于某些PHP 5.3版本中。 PHP 5.4没有任何显示。
Related PHP bug和我的作为在溶液(property_exists
忽略无效名称):
if(version_compare(PHP_VERSION, '5.4') < 0){
$props = array_filter($props, function($prop) use($obj){
return !$prop->isPublic() || property_exists($obj, $prop->name);
});
}
什么ArrayObjects? – sectus 2013-03-28 01:33:17