繁体   English   中英

Codeigniter-致命错误:在非对象上调用成员函数get_news()

[英]Codeigniter- Fatal error: Call to a member function get_news() on a non-object

当我尝试运行一个简单的函数时,它向我显示:

遇到PHP错误

严重程度:注意

消息:未定义的属性:Site :: $ model_users

文件名:controllers / site.php

行号:14

致命错误:在第14行的/hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php中的非对象上调用成员函数get_news()

功能:

位于models文件夹中的model_users.php:

public function get_news()
{
    $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
    return $query;
}

site.php位于controllers文件夹中:

public function home_news() {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
}

您是否只想在$this类上调用get_news()方法?

尝试

class Site {
   public function home_news() {
        $class_name = new Class_Name();  //change Class_Name() to the class that has the method get_news.
        $query = $class_name->get_news(); //This line caused the problem
        ...
        ...
   }
}

希望这可以帮助。

这应该是这样的:

模型:

class Model_users extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_news()
    {
        $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
        return $query;
    }
}


控制器:

class Site extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('model_users');
    }

    public function home_news() 
    {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM