doutong4088
doutong4088
2013-11-26 04:46

在第11行的C:\ xampp \ htdocs \ CodeIgniter_Practice \ application \ controllers ews.php中的非对象上调用成员函数get_news()

已采纳

I wrote a small application from CodeIgnitor user guide but when I run it, display the given message

Fatal error: Call to a member function get_news() on a non-object in C:\xampp\htdocs\CodeIgniter_Practice\application\controllers
ews.php on line 11

The code is

class News extends CI_Controller{
  public function _construct()    
  {
     parent::_construct();   
     $this->load->model('news_model');
  }
  public function index()
  {
  $data['news'] = $this->news_model->get_news(); 
     $data['title'] = 'News archive';

     $this->load->view('templates/header',$data);
     $this->load->view('news/index',$data);
     $this->load->view('templates/footer');
  }
}

Line 11 is :

$data['news'] = $this->news_model->get_news();
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douyan6871 douyan6871 8年前

    By looking at your code, i could see that you've missed one '_'(underscore) while defining your construct. It has to be as below:

    public function __construct()    
    {
      parent::__construct();   
      $this->load->model('news_model');
     }  
    
    点赞 评论 复制链接分享
  • drcb19700 drcb19700 8年前

    You can call your model by getting your Instance as well:

    class News extends CI_Controller{
     public function __construct()    
     {
       parent::__construct();
       $this->CI = & get_instance();   
       $this->CI->load->model('news_model');
     }
     public function index()
     {
       $data['news'] = $this->CI->news_model->get_news(); 
       $data['title'] = 'News archive';
    
       $this->load->view('templates/header',$data);
       $this->load->view('news/index',$data);
       $this->load->view('templates/footer');
     }
    }
    
    点赞 评论 复制链接分享