如何检查 JFactory::getSession() 中的会话是否已设置?

the*_*mhz 2 php joomla joomla2.5

viewform.php鉴于组件,我在 joomla 2.5 中有以下几行

 $session = & JFactory::getSession();
 if(empty($session->get('MasterIndex'))) $session->set('MasterIndex',0);
Run Code Online (Sandbox Code Playgroud)

我得到

致命错误:无法在 /var/www/ 中的写入上下文中使用方法返回值。

我也尝试过

 if(empty($session->get('MasterIndex'))) $session->set('MasterIndex',0);
Run Code Online (Sandbox Code Playgroud)

如何检查会话值是否已设置?

Sud*_*oti 5

尝试:

$session =& JFactory::getSession();
$setSession = $session->get('MasterIndex');
if(empty($setSession)) $session->set('MasterIndex',0);
Run Code Online (Sandbox Code Playgroud)

注意:您收到该错误是因为empty()仅检查变量,因为其他任何内容都会导致解析错误