aszx0413
(Michael Lee)
1
With code: var_dump(Yii::$app->session)
It dump an object:
object(yii\web\Session)#109 (6) {
["flashParam"]=>
string(7) "__flash"
["handler"]=>
NULL
["_cookieParams":"yii\web\Session":private]=>
array(1) {
["httponly"]=>
bool(true)
}
["_hasSessionId":"yii\web\Session":private]=>
NULL
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL
}
Then I dont understand why var_dump(Yii::$app->session[‘test’]) can get a key ‘test’ value because this is the array usage.
Hope someone can help me out. Thx.
sball
(Sebastiangolian)
2
Is not array.
This is object of class yii\web\DbSession.
Tray this:
Yii::$app->session->set('test','hello world');
if (Yii::$app->session->has('test'))
{
echo Yii::$app->session->get('test');
}
aszx0413
(Michael Lee)
3
But why Yii::$app->session[‘test’] will get the value correctly?
And the doc says:
// get a session variable. The following usages are equivalent:
$language = $session->get('language');
$language = $session['language'];
softark
(Softark)
4
It is because Session implements ArrayAccess interface.
http://php.net/manual/en/class.arrayaccess.php