ZF-800: After adding an array to a session namespace, adding elements to the array fails

Issue Type: Bug Created: 2007-01-25T22:18:46.000+0000 Last Updated: 2007-07-31T13:36:10.000+0000 Status: Closed Fix version(s): - 0.8.0 (21/Feb/07)

Reporter: Gavin (gavin) Assignee: Gavin (gavin) Tags: - Zend_Session

Related issues: - ZF-1743



-------- Original Message -------- Subject: [fw-auth] Problem with Zend Session and setting arrays Date: Thu, 18 Jan 2007 07:00:40 -0800 (PST) From: Daniel Schwip. (Germany) To:

Hi there, if i set a session property to an array and later add a new key, it won't be set. I think it's because of the __set php implementation???

$session = new Zend_session(); $session->array = array(); $session->array['testKey'] = 1; Zend::Dump($session->array, 'sessArray'); // testKey is not set // My Current workaround $session->array = array_merge($session->array, array('testKey' => 1));


Posted by Gavin (gavin) on 2007-01-26T10:50:54.000+0000

Oddly, I found setting additional keys in the array works, with only the first one "ignored" by PHP. The problem certainly appears related to PHP and not Zend_Session. Thus, a temporary workaround for the code above:

<pre class="highlight">
$session->array['dummy''] = true;
$session->array['testkey'] = 1;

$ php -v PHP 5.1.6 (cgi-fcgi) (built: Oct 24 2006 19:38:28) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies $ svn update uname At revision 3015. $ uname -a Linux 2.6.9-34.EL #1 Fri Feb 24 16:44:51 EST 2006 i686 i686 i386 GNU/Linux

Posted by Gavin (gavin) on 2007-01-26T11:14:10.000+0000

The result differs for PHP 5.2, and the "trick" above no longer works. All keys added are "ignored" by PHP.

$ php -v PHP 5.2.0 (cgi-fcgi) (built: Dec 19 2006 15:37:32) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session $ uname -a CYGWIN_NT-5.1 IBM-F4F0A5528C1 1.5.22(0.156/4/2) 2006-11-13 17:01 i686 Cygwin

gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session $ svn update At revision 3016.

Posted by Gavin (gavin) on 2007-01-29T16:27:53.000+0000

The following code produces inconsistent results on versions of PHP prior to PHP 5.2.1. Tests with 5.2.1RC4 seem to show the problem has been fixed with magic methods returning by reference.

<pre class="highlight"><?php


$_SESSION['anArray'] = array('a', 'b', 'c');
echo "\n\$_SESSION = ";

class TestSessionRef
    public function &__get($name)
        echo "\nReturning \$_SESSION[$name] by reference\n";
        return $_SESSION[$name];

$test = new TestSessionRef();
$test->anArray['foo'] = 'bar';
echo "\n\$test->anArray = ";

echo "\n\$a = ";
$a = & $test->anArray;
$a['bee'] = 'honey';

echo "\n\$_SESSION = ";

h2. Work-around

Do not modify an array, after assigning it to a session namespace key.

Posted by Gavin (gavin) on 2007-01-30T13:29:14.000+0000

The new Zend_Session unit test passes with PHP 5.2.1RC4 (and hopefully will continue to pass with PHP 5.2.1+):

If no further problems are found, after PHP 5.2.1 is released, then I will close this issue.

Everyone using an earlier version of PHP should use the workaround above.

Posted by Gavin (gavin) on 2007-01-30T15:18:38.000+0000

h2. Another Work-Around

<pre class="highlight">$myNamespace = new Zend_Session_Namespace('mySpace');

// works, even for broken versions of PHP
$a = array(1,2,3);
$myNamespace->someArray = array( & $a ) ;
$a['foo'] = 'bar';

Posted by Stephen St. Martin (kuprishuz) on 2007-02-21T16:25:03.000+0000

5.2.1 has been released, and i have confirmed the issue no longer exists, this could probably be closed.

Have you found an issue?

See the Overview section for more details.


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

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