views:

76

answers:

1

Hello! I have some example of pushing array to array in session object:

   class someClass extends someOtherOne {

...////// some other code here that starts session and  creates namespace

    public function __add2Session($a,$b) {  

    $namespc = $this -> __getnewNameSpace(); //returns a Zend Session Namesapce (object)

        if (!isset($namespc -> {$a})) { $namespc -> {$a} = array(); }

            array_push($namespc -> {$a}, $b);

        }

    }


.../////////////


$item=array(1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six",7=>"seven");

$k = new someClass();
$cart = new Zend_Session_Namespace('Cart');
$k -> __add2Session("items",$item);

The result is when I reload the page several times - the value of $cart -> items in the session gets overwritten and not populated. Can somebody explain why it occurs and how do I fix this?

I want to have $cart -> items to be an "array in array" like:

$cart -> items = array(array(1=>"one",2=>"two"), array(1=>"two",2=>"three"));
+1  A: 

Which PHP version are you using?

It could be that you have to get the array first and reassign it after manipulation:

$tmp = $namespc->{$a};
array_push($tmp, $b);
$namespc->{$a} = $tmp;

I suggest to read Zend_Session - Working with Arrays and follow their examples.

Update:

As you use PHP 5.2, you might have to do as I proposed. In the documentation it says:

Due to the implementation history of PHP magic methods, modifying an array inside a namespace may not work under PHP versions before 5.2.1.

Felix Kling
PHP version 5.2
@kate-koopy: Then this is probably the problem.
Felix Kling
@Fekix Kling I just look my exact version is 5.2.6 and as to http://framework.zend.com/issues/browse/ZF-800 bug should not occur. But it does :(