【问题标题】:Problems while using Factory, Abstract class and Models in CodeIgniter在 CodeIgniter 中使用工厂、抽象类和模型时出现的问题
【发布时间】:2011-09-09 11:26:05
【问题描述】:

我正在使用 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 中编写它的最佳方式吗?需要帮助我真的很困惑。

【问题讨论】:

    标签: php model-view-controller codeigniter factory abstract


    【解决方案1】:

    如果基类直接在其他模型旁边的模型中,只需在开始类代码之前添加require_once( 'filename.php' );

    如果您的基类位于库(或其他文件夹)中,我建议您移动它,因为我认为该位置不合适。如果我建立一个基础模型类,我把它放在模型目录中......

    【讨论】:

    • 我不能,user_m 扩展了模型,所以我不能只包含它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多