ZF2-431: Zend\Session\Container does not allow modification by reference
Description
Because of the "ArrayObject returns value rather than reference" issue (see, for example, https://bugs.php.net/bug.php?id=51622 ), the Zend\Session\Container does not allow certain types of modifications of properties/array elements.
Here are some unit tests and responses that demonstrate the issue:
public function testContainerAllowsArrayModification()
{
$this->container['foo'] = 1;
$this->assertEquals(1, $this->container['foo']);
$this->container['foo']++;
$this->assertEquals(2, $this->container['foo']);
}
There was 1 error:
1) ZendTest\Session\ContainerTest::testContainerAllowsArrayModification Indirect modification of overloaded element of Zend\Session\Container has no effect
public function testContainerAllowsObjectModification()
{
$this->container->foo = 1;
$this->assertEquals(1, $this->container->foo);
$this->container->foo++;
$this->assertEquals(2, $this->container->foo);
}
There was 1 failure:
1) ZendTest\Session\ContainerTest::testContainerAllowsObjectModification Failed asserting that 1 matches expected 2.
Comments
Posted by Matthew Weier O'Phinney (matthew) on 2012-07-30T20:55:50.000+0000
Interestingly, you can do the following fine:
Considering that internally we're using an ArrayObject, the liklihood of getting this to work with increment and decrement operators is pretty slim. However, since increment/decrement on assignment works fine, I'd argue we should simply document those use cases.
Posted by Matthew Weier O'Phinney (matthew) on 2012-07-30T20:56:22.000+0000
Updating issue type to "documentation task".
Posted by Demian Katz (demiankatz) on 2012-07-31T12:36:59.000+0000
Increment/decrement was just the simplest use case I could think of that caused a problem. Possibly more serious is the problem of appending to existing arrays:
(These trigger errors equivalent to the ones listed in the ticket above).
I agree that ```. (You can't use += in this context -- it yields an "Unsupported operand types" fatal error).
Posted by Matthew Weier O'Phinney (matthew) on 2012-07-31T13:22:05.000+0000
Right, but, again, because it's an ArrayObject implementation internally, you can do this:
As such, I still feel this falls into a documentation issue -- we need to clarify that the container and its members follows ArrayObject behavior, which means you have to treat it as such when using it.
Posted by Demian Katz (demiankatz) on 2012-07-31T13:48:56.000+0000
Makes sense -- thanks for the clarification. (I'm still not totally comfortable with ArrayObject behavior, but that's not ZF's problem!).
Posted by Ralph Schindler (ralph) on 2012-10-08T20:15:05.000+0000
This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2510