|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-08-04 14:45 UTC] william dot bailey at cowboysfromhell dot co dot uk
Description:
------------
Reflection information returned by ReflectionObject::getProperties() is corrupt when an object has numeric properties.
Reproduce code:
---------------
<?php
function d($o)
{
print get_class($o).PHP_EOL.PHP_EOL;
$r = new ReflectionObject($o);
foreach($r->getProperties() as $p)
{
var_dump($p);
print 'Name (HEX): '. bin2hex($p->getName()).PHP_EOL;
print PHP_EOL.PHP_EOL;
}
}
d((object) array('foo'=>'zero', '56'=>'one', 'bar'=>'two'));
Expected result:
----------------
stdClass
object(ReflectionProperty)#3 (2) {
["name"]=>
string(3) "foo"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 666f6f
object(ReflectionProperty)#4 (2) {
["name"]=>
string(2) "56"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 3536
object(ReflectionProperty)#5 (2) {
["name"]=>
string(3) "bar"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 626172
Actual result:
--------------
stdClass
object(ReflectionProperty)#3 (2) {
["name"]=>
string(3) "foo"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 666f6f
object(ReflectionProperty)#4 (2) {
["name"]=>
string(9) "rties() !"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 727469657328292021
object(ReflectionProperty)#5 (2) {
["name"]=>
string(3) "bar"
["class"]=>
string(8) "stdClass"
}
Name (HEX): 626172
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 20:00:01 2025 UTC |
I agree that 56 from a user defined class perspective is an invalid property name. However for anonymous or dynamic classes we have a number of ways to create numeric, and I assume other invalid properties on an object. If a property can be created, even if its invalid then surely it should be visible using reflection otherwise it will only confuse people as the output from a simple var_dump will not match what reflection will return. From the top of my head invalid properties can be created in the following ways... <?php $o = json_decode('{"foo":"bar","56":"FitfySix"}'); var_dump($o); $o = (object) array("foo"=>"bar","56"=>"FiftySix"); var_dump($o); $o = new stdClass(); $o->foo = 'bar'; $o->{56} = 'FiftySix'; var_dump($o); $n = 56; $o = new stdClass(); $o->foo = 'bar'; $o->$n = 'FiftySix'; var_dump($o);