用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6947|回复: 10
收起左侧

Message: Undefined property: News::$new_model

[复制链接]
发表于 2012-8-22 22:35:57 | 显示全部楼层 |阅读模式
本帖最后由 猪头三君 于 2012-8-22 22:35 编辑

大家好,我第一次接触PHP及CodeIgniter框架,今天按照教程搭建一个简单的程序,但是在在加载页面时出现一个如下的错误:

A PHP Error was encounteredSeverity: Notice
Message: Undefined property: News : : $new_model
Filename: controllers/news.php
Line Number: 9


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

我的代码:
controllers/news.php
PHP复制代码
 
<?php
 class News extends CI_Controller{
  public function _construct(){
    parent::_construct();
    $this->load->model('News_Model');
   }
   
  public function index(){
   $data['news'] = $this->new_model->get_news();//错误出现在这行
   $data['title'] = 'News archive';
   
   $this->load->view('templates/header', $data);
   $this->load->view('news/index', $data);
   $this->load->view('templates/footer');
  }
 
  public function view($slug){
   $data['news'] = $this->new_model->get_news($slug);
   
  }
 }
?>
 
复制代码


models/news_model.php
PHP复制代码
 
<?php
 class News_Model extends CI_Model{
  public function _construct(){
   $this->lode->database();
   }
   
   
  public function get_news($slug=FALSE){
   if($slug===FALSE){
    $query = $this->db->get('news');
    return $query->result_array();
   }
   $qusry = $this->db->get_where('news',array('slug'=>$slug));
   return $query->row_array();
  }
 }
?>
 
复制代码


以上完全是教程里的代码,但是我这里会报错;求高手指点,万分感谢。

我也搜索了相关问题,得到一个解决方法是:
修改autoload.php 文件中的$autoload['libraries'] = array('database');
但是,对我无效。

发表于 2012-8-23 07:44:58 | 显示全部楼层
$this->load->model('news_model');

class News_model extends ....

改成这样试试
发表于 2012-8-23 08:33:00 | 显示全部楼层
$this->news_model->get_news才对,你把模型的名字写错了,少了一个s
 楼主| 发表于 2012-8-23 23:20:17 | 显示全部楼层
谢谢楼上两位的回答,但是我修改错误后,问题依然存在
发表于 2014-3-5 22:27:53 | 显示全部楼层
我也是这个问题,报同样的错,完全照着教程敲的,没敲错,谁知道这是为什么啊
发表于 2014-3-6 08:57:06 | 显示全部楼层
Android将军 发表于 2014-3-5 22:27
我也是这个问题,报同样的错,完全照着教程敲的,没敲错,谁知道这是为什么啊 ...

file:///C:\Users\gongchao\Documents\Tencent Files\1429602494\Image\Image2\GRAJDZ_I`OA{1107{%XOKJA.jpg

此问题解决方法

此问题解决方法
发表于 2014-4-5 13:15:31 | 显示全部楼层
改成__construct(),前面有两条下划线,而不是一个下划线
发表于 2014-7-14 11:17:00 | 显示全部楼层
#在这里快速回复#Parse error: syntax error, unexpected T_PUBLIC in C:\mycx\Apache2\Ci\application\controllers\news.php on line 20
发表于 2014-7-14 11:19:02 | 显示全部楼层
Parse error: syntax error, unexpected T_PUBLIC in C:\mycx\Apache2\Ci\application\controllers\news.php on line 20
这个错误是什么原因, 这一行的代码是: public function index()
{
按手册写的呀,语法错在哪呀,求教
发表于 2019-3-14 18:10:08 | 显示全部楼层
$this->lode->database(); 把这个写到getnews中 把getnews的构造函数删掉 试试

点评

...老铁。你看看帖子几时发的?  发表于 2019-3-18 09:22

本版积分规则