【发布时间】:2011-05-08 02:26:13
【问题描述】:
全部。这里是这样的情况,我有一个php页面,它正在做一些注册,登录,这些与用户相关的事情,所以,我们称之为user.php。
在user.php中,我有一个用户类有以下方法:
-public static function register($aEmail, $aPassword)
-public static function login($aEmail, $aPassword)
-public static function logout($aEmail, $aSessionKey)
所以,当用户登录时,我会做这样的事情:
if(isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["action"])){
$email = htmlspecialchars($_POST["email"]);
$password = htmlspecialchars($_POST["password"]);
if($_POST["action"] == login){
$user = new User();
//It will print the session key
$user->doLoginAndPrintTheSessionKey($email, $password);
}
效果很好,但问题是,我应该将用户作为对象还是仅作为单例,似乎没有必要保留用户对象,因为当用户提出请求时,我需要使用他的电子邮件和会话找到处理他的记录的关键.....
像这样..
public static function postAComment($aEmail, $aSessionKey, $aCommment){
BOOL $isSuccess = FALSE;
//check the session key is valid or not
if(self::isUserValidationValid($aEmail, $aSessionKey)){
//make a sql statement that write aComment to DB
//execute the sql statement
//if execute success, return isSuccess = TRUE;
}
return $isSuccess;
}
如您所见,我可以在单例类中完成所有这些操作,所以,我的问题是... ...在这种情况下不需要创建用户对象吗?谢谢。
【问题讨论】: