2014-09-19 61 views
0

在我的CodeIgniter應用程序中,我在某些控制器中使用了會話類(並非全部都是這樣),並且我修復了在所有頁面中呈現的主題視圖。我想在視圖中使用session->setflash()。因此,對於不使用會話的控制器,我必須檢查session->flashdata()是否已設置或可用。我曾嘗試follwoing:當我試圖消息的建議CodeIgniter中對象方法的等效isset是什麼?

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead)

<?php if (isset($this->session->flashdata('msg'))):;?> 

它返回以下錯誤

<?php if (null !== $this->session->flashdata('msg')):;?> 

我得到了以下錯誤:

Fatal error: Call to a member function flashdata() on a non-object in...

旁邊的代碼點火器錯誤:

A PHP Error was encountered Severity: Notice

Message: Undefined property: CI_Loader::$session

Filename: themes/head.php

Line Number: 44

回答

3

如果您的控制器總是加載$this->load->library('session');

然後,你可以只檢查if($this->session->flashdata('msg')){/*do stuff*/}

否則,你可以使用method_exists()

http://php.net/manual/en/function.method-exists.php

if(method_exists('CI_Session', 'flashdata') && $this->session->flashdata('msg')){ 
    echo $this->session->flashdata('msg'); 
} 
相關問題