Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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:如何从视图的根目录中获得$this?_Codeigniter_Codeigniter 3 - Fatal编程技术网

CodeIgniter:如何从视图的根目录中获得$this?

CodeIgniter:如何从视图的根目录中获得$this?,codeigniter,codeigniter-3,Codeigniter,Codeigniter 3,我知道$this指的是当前对象 但是,通常,在文件根目录中引用$this(不在对象中)会导致错误: PHP Fatal error: Using $this when not in object context in - on line 2 …但从某种角度来看,我有以下代码: <?php $this->load->view('templates/header'); $this->load->view($view); $this->load->view

我知道$this指的是当前对象

但是,通常,在文件根目录中引用$this(不在对象中)会导致错误:

PHP Fatal error:  Using $this when not in object context in - on line 2
…但从某种角度来看,我有以下代码:

<?php
$this->load->view('templates/header');
$this->load->view($view);
$this->load->view('templates/footer');

在控制器中调用
$this->load->view()
时,使用PHP的
include
命令将视图文件带入控制器类。因此视图的代码成为控制器类的一部分-成为
$this
范围的一部分

在控制器中调用
$this->load->view()
时,使用PHP的
include
命令将视图文件带入控制器类。因此视图的代码成为控制器类的一部分-成为
$this
范围的一部分

视图中的代码加载到对
eval
的调用中,或通过
加载程序
类的
\u ci\u load
方法中的
包含
。该方法还获取
CI
实例

请参阅
/system/core/Loader.php

在第920行获取实例

$_ci_CI =& get_instance();
将视图加载到第969行的
eval
include

// If the PHP installation does not support short tags we'll
// do a little string replacement, changing the short tags
// to standard PHP echo statements.
if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
{
    echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
}
else
{
    include($_ci_path); // include() vs include_once() allows for multiple views with the same name
}
//如果PHP安装不支持短标记,我们将
//做一点字符串替换,更改短标记
//到标准的PHP echo语句。
如果(!is_php('5.4')&&!ini_get('short_open_tag')&&config_item('rewrite_short_tags')==TRUE)
{

echo eval(“?>”。preg\u replace(“/;*\s*\?>”,“;?>”,str\u replace(“视图中的代码加载到对
eval
的调用中,或通过
加载程序
类的
\u ci\u load
方法中包含
。该方法还获取
ci
实例

请参阅
/system/core/Loader.php

在第920行获取实例

$_ci_CI =& get_instance();
将视图加载到第969行的
eval
include

// If the PHP installation does not support short tags we'll
// do a little string replacement, changing the short tags
// to standard PHP echo statements.
if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
{
    echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
}
else
{
    include($_ci_path); // include() vs include_once() allows for multiple views with the same name
}
//如果PHP安装不支持短标记,我们将
//做一点字符串替换,更改短标记
//到标准的PHP echo语句。
如果(!is_php('5.4')&&!ini_get('short_open_tag')&&config_item('rewrite_short_tags')==TRUE)
{

echo eval(“?>”.preg\u replace(“/;*\s*\?>/”,“;?>”,str\u replace(“
$this->load->view(file)
表示
包含文件
$this->load->view(file)
意思是
包含文件
我希望我能接受两个答案,但这一个更完整。我希望我能接受两个答案,但这一个更完整。我希望我也能接受这一个,因为它是准确的。我希望我也能接受这一个,因为它是准确的。