在CodeIgniter中使用工厂、抽象类和模型时的问题


Problems while using Factory, Abstract class and Models in CodeIgniter

我正在使用codeigniter,我想使用我的工厂库(或者如果你知道更好的方法)来创建用户,用户是一个扩展抽象模型的类。我得到了一个错误,我没有包括抽象类。我不知道如何将它包含在codeigniter中。我不能只加载一个扩展模型的抽象类。这是我的代码:

    class User extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('factory');
    }
    function register(){
        $user = $this->factory->create('doctor'); // returns 'doctor_m' or 'patient_m'
        $this->load->model($user); 
        $this->$user->addUser();
    }
}
abstract class User_m extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    abstract protected function addUser();
    abstract protected function getUser();
}
class Doctor_m extends User_m{
    function __construct() {
        parent::__construct();
    }
    function addUser() {
        echo "doctor";
    }
    function getUser() {
    }
}
class Patient_m extends User_m{
    function __construct() {
        parent::__construct();
    }
    function addUser() {
        echo 'patient';
    }
    function getUser() {
    }
}
class factory{
    function create($type){
        if($type == 'doctor') return 'doctor_m';
        else return 'patient_m';
    }
} 

这是怎么回事?这是用MVC编写它的最佳方式吗?需要帮助我真的很困惑。

如果基类与其他模型一起直接在模型中,则只需在开始类代码之前添加require_once( 'filename.php' );即可。

如果你的基类在库(或另一个文件夹)中,我建议你把它移走,因为我认为这不是一个合适的位置。如果我构建一个基本模型类,我会把它放在模型目录中。。。。