Php Codeigniter-致命错误:对非对象调用成员函数get_news()
当我尝试运行一个简单函数时,它会显示: 遇到一个PHP错误 严重性:通知 消息:未定义属性:站点::$model\u用户 文件名:controllers/site.php 电话号码:14 致命错误:对中的非对象调用成员函数get_news /hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php 第14行 职能: model_users.php位于models文件夹中:Php Codeigniter-致命错误:对非对象调用成员函数get_news(),php,function,codeigniter,fatal-error,Php,Function,Codeigniter,Fatal Error,当我尝试运行一个简单函数时,它会显示: 遇到一个PHP错误 严重性:通知 消息:未定义属性:站点::$model\u用户 文件名:controllers/site.php 电话号码:14 致命错误:对中的非对象调用成员函数get_news /hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php 第14行 职能: model_users.php位于models文件夹中: public functio
public function get_news()
{
$query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
return $query;
}
位于控制器文件夹中的site.php:
public function home_news() {
$query = $this->model_users->get_news(); //This line causes the problem
...
...
}
您不想在$this类上调用get_news方法吗 试一试 希望这能有所帮助。应该是这样的: 型号: 控制器:
我想从site.php调用model_users.phpHow中的函数。phpHow是被调用的具有get_news方法的类吗?你能不能再给我一些代码来显示这个类,这样会有帮助?你的错误是什么?我只是忘了写这行:$this->load->model\u users;
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
...
...
}
}