2013-01-04 30 views
-3

可能重複延伸CI_Model時:
Codeigniter Message: Undefined property: stdClass未定義場所在笨

位置:應用/核心/ student_model.php

class Student_model extends CI_Model 
{ 
    private $id; 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function setId($id){ 
     $this->id = $id; 
    } 

    public function getId() { 
     return $this->id; 
    } 
} 

位置:應用/對照ollers/test.php的

class Test extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('Student_model'); 
    } 

    public function index() 
    { 
     $student = $this->student_model->setId('1234567'); 
     echo $student->getId(); 
    } 
} 

我收到以下消息/錯誤。

Message: Undefined property: Test::$student_model 
Filename: controllers/test.php 
Line Number: 13 

這行是我調用方法setId的地方。

任何人都可以看到我做錯了什麼?

回答

3

嘗試

$this->Student_model->setId('1234567'); 

你的類是資本替代

$this->student_model->setId('1234567'); 

,所以物業也應AFAIK資本。

1

嘗試

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('Student_model', 's_model'); 
} 

public function index() 
{ 
    $student = $this->s_model->setId('1234567'); 
    echo $student->getId(); 
} 
0

你在做什麼錯的是:

你是$this->student_model->setId('1234567');
這是一個函數或「二傳手」不返回任何分配$student;你將無法使用$student->getId();

我會做的是

class Test extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('student_model', 'student'); 
    } 

    public function index() 
    { 
     $this->student->setId('1234567'); 
     echo $this->student->getId(); 
    } 
}