【发布时间】:2012-06-17 04:09:27
【问题描述】:
我想明确这一点。我正在使用 codeigniter。
在构建 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 技能需要提高。
非常感谢!
【问题讨论】:
-
感谢您的所有回答。 @painkiller,是的,我已经在使用 PhpActiveRecord 将我的数据库映射到我的模型。我正在用 Bonfire 做这个。
标签: php oop codeigniter object