Tommy Montgomery | 24 Jan 2010 00:01
Picon

Patch for PHP bug: accessing elements in objects cast to arrays

I wrote a patch for PHPUnit for the (known) PHP bug outlined here: 
http://bugs.php.net/bug.php?id=46758. Basically, if you have an object 
with numeric properties and then cast to an array, accessing those 
properties is difficult and buggy. Here's an example script that 
illustrates this:

$foo = new stdClass();
$foo->{0} = 'foo';
$a = (array)$foo;
var_dump($a, key($a), array_key_exists('0', $a), 
array_key_exists(key($a), $a));

This will output

array(1) {
  ["0"]=>
  string(3) "foo"
}
string(1) "0"
bool(false)
bool(false)

Anyway, I wrote a patch for the IsEqual constraint to handle these weird 
situations, but I noticed that PHPUnit is now on Git rather than SVN, 
and I don't really want to spend a couple hours right now learning Git, 
so I thought I'd just send the patch here. The patch is attached and is 
for PHPUnit 3.4.9.

Thanks,
Tommy
(Continue reading)

Sebastian Bergmann | 28 Jan 2010 09:37
Picon
Gravatar

Re: Patch for PHP bug: accessing elements in objects cast to arrays

Tommy Montgomery wrote:
> Anyway, I wrote a patch for the IsEqual constraint to handle these weird
> situations, but I noticed that PHPUnit is now on Git rather than SVN,
> and I don't really want to spend a couple hours right now learning Git,
> so I thought I'd just send the patch here. The patch is attached and is
> for PHPUnit 3.4.9.

 Did you have a look at my modified patch (http://tr.im/LRcp)?

--

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

Tommy Montgomery | 28 Jan 2010 10:15
Picon

Re: Patch for PHP bug: accessing elements in objects cast to arrays

Sebastian Bergmann wrote:
> Tommy Montgomery wrote:
>   
>> Anyway, I wrote a patch for the IsEqual constraint to handle these weird
>> situations, but I noticed that PHPUnit is now on Git rather than SVN,
>> and I don't really want to spend a couple hours right now learning Git,
>> so I thought I'd just send the patch here. The patch is attached and is
>> for PHPUnit 3.4.9.
>>     
>
>  Did you have a look at my modified patch (http://tr.im/LRcp)?
>
>   

Yeah, sorry, just never had a chance to try it out.

It didn't quite work, but with a bit of tweaking it seemed to work out 
all right. It's at least less intrusive then my original patch.

--- IsEqual-old.php     2010-01-28 01:05:14.782959800 -0800
+++ IsEqual.php 2010-01-28 01:12:49.499968200 -0800
 <at>  <at>  -333,8 +333,17  <at>  <at> 
             }
         }

+        $keysInB = array_flip(array_keys($b));
+
+        if (!empty($a)) {
+            $a = array_combine(array_keys($a), array_values($a));
+        }
(Continue reading)


Gmane