php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49154 Corrupted reflection information when object property is numeric
Submitted: 2009-08-04 14:45 UTC Modified: 2009-08-05 09:28 UTC
From: william dot bailey at cowboysfromhell dot co dot uk Assigned:
Status: Closed Package: Reflection related
PHP Version: 5.3.0 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: william dot bailey at cowboysfromhell dot co dot uk
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-04 14:59 UTC] jani@php.net
Already fixed in svn. And it will only return 2 properties. '56' is not valid property name.
 [2009-08-05 09:28 UTC] william dot bailey at cowboysfromhell dot co dot uk
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);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC