【发布时间】:2013-06-05 20:57:17
【问题描述】:
我不确定我的代码有什么问题......它在加载模型时导致错误...... 请帮忙........... 我的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Exams extends CI_Controller {
public function index(){
$ob = new exam_class();
$ob->set_exam(0,'Html exam','1','20');
$ob->create_exam();
echo 'success';
}
}
class exam_class{
private $id;
private $title;
private $catagory;
private $timeLength;
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->load->model('examModel');
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
型号
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ExamModel extends CI_Model {
public function create_exams($title,$catagory,$timeLength){
$data = array(
'title' => $title ,
'catagory' => $catagory ,
'timeLength' => $timeLength
);
$this->db->insert('exams', $data);
}
}
错误 遇到 PHP 错误
严重性:通知
消息:未定义的属性:exam_class::$load
文件名:controllers/exams.php
行号:26
致命错误:在第 26 行的 C:\xampp\htdocs\exam\application\controllers\exams.php 中对非对象调用成员函数 model()
【问题讨论】:
-
为什么要在控制器中分离 2 个类?
-
我使用单独的类来简化我的站点控制器............只是创建另一个类的对象......我是框架中的新手......可能是我的编码方法不正确....请给我任何其他建议....我发现错误我的考试类没有扩展 CI_Controller thnx 以寻求您的帮助:)
-
尝试创建新实例来加载模型 $ci = get_instance(); $ci->load->model('模型名称');
标签: codeigniter