Zend Framework

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

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0.1
  • Fix Version/s: 1.6.0
  • Component/s: Zend_Db_Table
  • Labels:
    None
  • Fix Version Priority:
    Should Have

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

Activity

Hide
julien PAULI added a comment -

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://www.php.net/manual/en/function.ArrayObject-construct.php

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

Show
julien PAULI added a comment - 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://www.php.net/manual/en/function.ArrayObject-construct.php 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
Hide
Darby Felton added a comment -

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.

Show
Darby Felton added a comment - 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.
Hide
Wil Sinclair added a comment -

Please categorize/fix as needed.

Show
Wil Sinclair added a comment - Please categorize/fix as needed.
Hide
Wil Sinclair added a comment -

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

Show
Wil Sinclair added a comment - This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Hide
julien PAULI added a comment -

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).

Show
julien PAULI added a comment - 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).
Hide
Darby Felton added a comment -

Actually, this behavior is not documented here:

http://framework.zend.com/manual/en/zend.db.table.row.html#zend.db.table.row.read.to-array

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

Show
Darby Felton added a comment - Actually, this behavior is not documented here: http://framework.zend.com/manual/en/zend.db.table.row.html#zend.db.table.row.read.to-array I think now this should be fixed for the next mini-release.
Hide
julien PAULI added a comment -

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.

Show
julien PAULI added a comment - 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.
Hide
julien PAULI added a comment -

fixed at r9316

Show
julien PAULI added a comment - fixed at r9316
Hide
Wil Sinclair added a comment -

Updating for the 1.6.0 release.

Show
Wil Sinclair added a comment - Updating for the 1.6.0 release.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
1d
Original Estimate - 1 day
Remaining:
1d
Remaining Estimate - 1 day
Logged:
Not Specified
Time Spent - Not Specified