[英]Codeigniter- Fatal error: Call to a member function get_news() on a non-object
When I try to run a simple function it shows me: 当我尝试运行一个简单的函数时,它向我显示:
A PHP Error was encountered 遇到PHP错误
Severity: Notice 严重程度:注意
Message: Undefined property: Site::$model_users 消息:未定义的属性:Site :: $ model_users
Filename: controllers/site.php 文件名:controllers / site.php
Line Number: 14 行号:14
Fatal error: Call to a member function get_news() on a non-object in /hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php on line 14 致命错误:在第14行的/hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php中的非对象上调用成员函数get_news()
The functions: 功能:
model_users.php located in models folder: 位于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 located in controllers folder: site.php位于controllers文件夹中:
public function home_news() {
$query = $this->model_users->get_news(); //This line causes the problem
...
...
}
Don't you just want to call the method get_news()
on $this
class? 您是否只想在$this
类上调用get_news()
方法?
Try 尝试
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
...
...
}
}
Hope this helps. 希望这可以帮助。
This is how it should be: 这应该是这样的:
Model: 模型:
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;
}
}
Controller: 控制器:
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.