CodeIgniter:无法在视图中访问函数

时间:2011-06-04 01:43:50

标签: php codeigniter

我正在使用CodeIgniter,我的一个视图变得非常大,所以我将一些函数中的一些代码移动到同一个文件中:

function html_stuff()
{
    $posts = $this->db->query('select * from posts');
}

当我运行此代码时,我收到以下错误:

  

致命错误:不在时使用$ this   /somepath/view.php中的对象上下文

1 个答案:

答案 0 :(得分:7)

您可以传递函数$this

function html_stuff($ci) {
    $ci->db->query('select * from posts');
}
html_stuff($this);

或使用get_instance()

function html_stuff() {
    $ci = &get_instance();
    $ci->db->query('select * from posts');
}

请参阅:https://www.codeigniter.com/user_guide/general/creating_libraries.html

相关问题