加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:41:59 所属栏目:PHP教程 来源:网络整理
导读:我正在使用CodeIgniter,我的一个视图变得非常大,所以我在一个函数中移动了一些代码在同一个文件中: function html_stuff(){ $posts = $this-db-query('select * from posts');} 当我运行此代码时,我收到以下错误: Fatal error: Using $this when not in ob
我正在使用CodeIgniter,我的一个视图变得非常大,所以我在一个函数中移动了一些代码在同一个文件中:

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

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

Fatal error: Using $this when not in
object context in /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');
}

见:https://www.codeigniter.com/user_guide/general/creating_libraries.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读