ZF2-431: Zend\Session\Container does not allow modification by reference

Issue Type: Docs: Task Created: 2012-07-27T15:23:51.000+0000 Last Updated: 2012-10-08T20:15:05.000+0000 Status: Closed Fix version(s): Reporter: Demian Katz (demiankatz) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend\Session

Related issues: Attachments:


Because of the "ArrayObject returns value rather than reference" issue (see, for example, ), 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:

<pre class="highlight">
    public function testContainerAllowsArrayModification()
        $this->container['foo'] = 1;
        $this->assertEquals(1, $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->assertEquals(2, $this->container->foo);

There was 1 failure:

  1. ZendTest\Session\ContainerTest::testContainerAllowsObjectModification Failed asserting that 1 matches expected 2.


Posted by Matthew Weier O'Phinney (matthew) on 2012-07-30T20:55:50.000+0000

Interestingly, you can do the following fine:

<pre class="highlight">
$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.

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:

<pre class="highlight">
    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).

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:

<pre class="highlight">

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:

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.