Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Codeigniter在header.php中不在对象上下文中使用$this_Php_This_Codeigniter 3 - Fatal编程技术网

使用Codeigniter在header.php中不在对象上下文中使用$this

使用Codeigniter在header.php中不在对象上下文中使用$this,php,this,codeigniter-3,Php,This,Codeigniter 3,我刚从网上得到一个从数据库中获取数据的代码。这对我有用。但是在使用phpstorm进行调试时。我犯了这个错误。我正在header.php中使用$this来显示会话数据 <li class="dropdown" id="profile-messages" ><a title="" href="#" data-toggle="dropdown" data-target="#profile-messages" class="dropdown-toggle"> <

我刚从网上得到一个从数据库中获取数据的代码。这对我有用。但是在使用phpstorm进行调试时。我犯了这个错误。我正在header.php中使用
$this
来显示会话数据

<li  class="dropdown" id="profile-messages" ><a title="" href="#" data-toggle="dropdown" data-target="#profile-messages" class="dropdown-toggle">
    <i class="icon icon-user"></i>  
    <span class="text">Welcome 
    <?php echo $this->session->userdata('user_name'); ?></span><b class="caret"></b></a>

实际上,您正试图访问视图中始终无法访问的CodeIgniter类对象。请参考下面的代码

$CI =& get_instance();
echo $CI->session->userdata('username');

$CI用CodeIgniter类对象实例化,您将看到结果。

对不起,您的答案不是在问题标题中吗?“不在对象上下文中使用$this”?为什么不使用实例化的对象$这是用于内部类构造的。首先实例化你的对象,然后在你的页面中使用该实例。也许我不清楚错误发生在哪里,但是你遇到了什么问题以及期望的结果?可能是重复的
$CI =& get_instance();
echo $CI->session->userdata('username');