CodeIgniter:如何从视图的根目录中获得$this?
我知道$this指的是当前对象 但是,通常,在文件根目录中引用$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
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)
意思是包含文件
我希望我能接受两个答案,但这一个更完整。我希望我能接受两个答案,但这一个更完整。我希望我也能接受这一个,因为它是准确的。我希望我也能接受这一个,因为它是准确的。