$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