我想明确这一点。我正在使用代码点火器。
在构建 Web 应用程序时,在 codeigniter 中 OOP 设计的更好/正确/标准/最受赞赏的方式是什么/哪种?
一般来说,
一、创建一个像User这样的自定义类并具有登录功能?像,
class User{
var $db, $id, $username, $password, $role; //have all user attributes here
function __construct() {
//instance for accessing the database
$this->db =& get_instance();
}
public function login(){
//do the login here
$this->db->load->model('user_model');
$success = $this->db->user_model->login($this->username, $this->password);
//isn't this redundant using models? or should I rename the model function name like checkLogin?
return $success;
}
//other class functions
}
或者
二、直接从控制器调用模型中的登录函数而不创建用户对象?
o_o
在方案 1 中,我使模型仅在数据库中查询。我还可以让“用户”对象像“客户”、“管理员”一样被继承,并为每个对象添加特定功能。我的优势是让 CI 控制器专注于布局和将变量传递给视图,这主要是它的目的(不太确定),并让我的自定义类/对象处理我的 Web 应用程序中的操作。 但是我觉得我的登录功能有冗余,我在模型中调用了另一个登录功能。这个好吗?
在方案 2 中,让我的控制器处理所有操作(如登录、注册或创建日志并接收输入帖子并将变量传递给视图)是否合适?
我真的需要一些指导。我的 OO 技能需要提高。
非常感谢!