简体   繁体   English

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

[英]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.

相关问题 在非对象上调用成员函数get_news() - Call to a member function get_news() on a non-object 在第11行的C:\\ xampp \\ htdocs \\ CodeIgniter_Practice \\ application \\ controllers \\ news.php中的非对象上调用成员函数get_news() - Call to a member function get_news() on a non-object in C:\xampp\htdocs\CodeIgniter_Practice\application\controllers\news.php on line 11 致命错误:在非对象,代码点火器控制器上调用成员函数 - Fatal error: Call to a member function, on a non-object, codeigniter controller 致命错误:在Codeigniter中的非对象上调用成员函数result() - Fatal error: Call to a member function result() on a non-object in Codeigniter 致命错误:在(CodeIgniter)中的非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object in (CodeIgniter) codeigniter致命错误在非对象上调用成员函数cookie() - codeigniter fatal error Call to a member function cookie() on a non-object Codeigniter 致命错误:调用非对象上的成员函数 query() - Codeigniter Fatal error: Call to a member function query() on a non-object CodeIgniter致命错误:调用非对象上的成员函数display_clients() - CodeIgniter fatal error: Call to a member function display_clients() on a non-object Codeigniter / Postgresql:致命错误:在非对象上调用成员函数num_rows() - Codeigniter/Postgresql: Fatal error: Call to a member function num_rows() on a non-object CodeIgniter随机PHP致命错误:在... mysqli_driver.php中的非对象上调用成员函数。 - CodeIgniter Random PHP Fatal error: Call to a member function … on a non-object in … mysqli_driver.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM