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

Interestingly, you can do the following fine:


$container += 1;

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.

Updating issue type to "documentation task".

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:


    public function testContainerAllowsArrayModificationOfArrays()
    {
        $this->container['foo'] = array();
        $this->assertEquals(0, count($this->container['foo']));
        $this->container['foo'][] = 'bar';
        $this->assertEquals(1, count($this->container['foo']));
    }

    public function testContainerAllowsObjectModificationOfArrays()
    {
        $this->container->foo = array();
        $this->assertEquals(0, count($this->container->foo));
        $this->container->foo[] = 'bar';
        $this->assertEquals(1, count($this->container->foo));
    }

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

Right, but, again, because it's an ArrayObject implementation internally, you can do this:


$container->foo->append('bar');

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.

Makes sense -- thanks for the clarification. (I'm still not totally comfortable with ArrayObject behavior, but that's not ZF's problem!).

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