在Zend Framework 2 Sessions中添加类似数据的数组

时间:2013-01-06 15:23:51

标签: zend-framework2

我想知道如何实现以下目标:

我有一个工厂创建的会话容器:

        $container = new Container('Fans');
        $container->setExpirationSeconds('219867583');
        return $container;

然后我在我的控制器中创建一个像这样的实例:

$this->sessionService = $this->getServiceLocator()->get('SessionService');

现在我想在Session中添加一些内容:

这个很好用:

$this->sessionService->team = 'TEST';

但我想要实现的是以下

$this->sessionService->team[0] = 'Team Name 0' // This doesn't work;
$this->sessionService->team[1] = 'Team Name 1' // This doesn't work;
\Zend\Debug\Debug::dump($this->sessionService->team);

输出如下所示:

<pre>string(9) "TEST" </pre>

我不知道我是误解了什么或做错了什么。

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

这是PHP中魔术__get()功能的问题。由于Zend\Session使用__get()来提供对会话变量的访问,因此您无法像数组一样访问它们。有什么作用,如下:

$team = array();
$team[0] = 'Team Name 0';
$team[1] = 'Team Name 1';
$this->sessionService->team = $team;

答案 1 :(得分:0)

如何使用数组?

$teams = array();
$session->teams = $teams;
$session->teams[0] = 'blubb';

如果您首先将其指定为字符串,则它将保留为字符串。使它成为一个数组并将其用作数组;)