【问题标题】:What is the equivalent of isset for object's methods in CodeIgniter?CodeIgniter 中对象方法的 isset 等价物是什么?
【发布时间】:2014-09-19 13:26:12
【问题描述】:

在我的 CodeIgniter 应用程序中,我在一些控制器(不是全部)中使用会话类,并且我在所有页面中都呈现了固定的主题视图。我想在视图中使用session->setflash()。因此,对于不使用会话的控制器,我必须检查 session->flashdata() 是否已设置或可用。我尝试了以下方法:

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

它返回以下错误:

致命错误:无法对函数调用的结果使用 isset()(您 可以使用 "null !== func()" 代替)

当我尝试消息的建议时:

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

我收到以下错误:

致命错误:在非对象上调用成员函数 flashdata() 在...

除了 codeIgniter 错误:

遇到 PHP 错误 严重性:通知

消息:未定义属性:CI_Loader::$session

文件名:themes/head.php

行号:44

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    如果你的控制器总是加载$this-&gt;load-&gt;library('session');

    然后你就可以检查if($this-&gt;session-&gt;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');
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 1970-01-01
      • 2020-07-24
      • 2011-05-26
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多