Issues

ZF-1898: Zend_Db_Table_Row_Abstract::toArray returns a reference to data instead of an array of data

Description

Bill, I am noticing circular references when I try to return an array:

Ponder this:


$thing = new ArrayObject($row->toArray(), ArrayObject::ARRAY_AS_PROPS);
$thing->foo= 'bar';

Also adds foo = bar into the _data of the $row.

Perhaps toArray() shoudl cast _data to (Array) before returning it?

-ralph

Comments

Yes, ArrayObject's constructor make a reference on the variable passed to it. Casting to array before returning, explicitly tells PHP to make a real copy (not a COW) of the returned array.

See the first comment at http://php.net/manual/en/…

I don't know if this sould be considered as a ZF issue, or a PHP issue, it's actually confused, as you can read in that bug report : http://bugs.php.net/bug.php?id=42065

I agree that {{toArray()}} should return a copy, rather than a reference to the object's properties. A change to a member of the returned value of {{toArray()}} that results in changes to the object data seems like unintended behavior.

Since this would be a slight BC break, however, and because I would like to hear Bill's comments on the issue, I remove 1.0.2 from fix version list.

Please categorize/fix as needed.

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

I cant reproduce on PHP5.2.5, seems to have been fixed in 5.2.4 but not tested :


$a = array('foo'=>'bar');
$arrayobject = new ArrayObject($a,ArrayObject::ARRAY_AS_PROPS);
$arrayobject->foo = 'foobar';
assert($a['foo'] == 'foobar'); // fails on 5.2.5

if you pass a reference $arrayObject (&$a) , then assertion pass (and a logical call-time pass reference warning appears)

Please confirm, I have a patch for that issue (if necessary).

Actually, this behavior is not documented here:

http://framework.zend.com/manual/en/…

I think now this should be fixed for the next mini-release.

I confirm that this is a PHP issue that comes out in PHP <= 5.2.3 (fixed in 5.2.4) please refer to http://bugs.php.net/bug.php?id=42065 for more informations.

So it needs to be patched, and won't BC anything

I'm on it.

fixed at r9316

Updating for the 1.6.0 release.