CodeIgniter:不能访问$ this在函数内的视图 [英] CodeIgniter: can't access $this within function in view

查看:98
本文介绍了CodeIgniter:不能访问$ this在函数内的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter,我的一个视图变得相当大,所以我移动了一些函数中的一些代码在同一个文件:

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

当我运行这个代码时,我得到以下错误:


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



解决方案

您可以传递函数 $ 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');
}

请参阅: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html


I'm using CodeIgniter and one of my views got pretty large so I moved some of the code in a function in the same file:

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

When I run this code I get the following error:

Fatal error: Using $this when not in object context in /somepath/view.php

解决方案

You could either pass the function $this

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

Or use get_instance()

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

See: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html

这篇关于CodeIgniter:不能访问$ this在函数内的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆