简体   繁体   中英

ZF2 - How are you supposed to access session vars in a session container?

I create a container like so:

$frontend = new SessionContainer('frontend', null);

I set a variable like so:

$frontend->offsetSet('foo',$bar);

My question is, when you need to access this somewhere else, are you really supposed to instantiate a new SessionContainer with the same key everywhere you go and grab the var? Or, is the session data being passed around in another variable available in the controller or something?

The preferred practice is to directly access the session values as if they were properties of the instantiated container object.

$frontend = new SessionContainer('frontend');
$bar = $frontend->foo;

The session container class does the rest of the work behind the scenes by calling:

$frontend->__get('foo');

which in turn calls:

$frontend->offsetGet('foo');

Using the first example above helps improve readability. By the same token you can use:

$frontend->foo = 'bar';

Behind the scenes this calls:

$frontend->__set('foo', 'bar');

... and then:

$frontend->offsetSet('foo', 'bar');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM