0

我正在构建一个面向对象的表单系统。我正在整理一个包含大约八个函数的用户类。每个函数都包含一个 MySQL 查询,必须为其实例化 Query 类的对象。

有什么办法可以避免每次都声明一个新对象?在我看来,可能会在某些时候使服务器陷入困境。

User 类的作用是从数据库中提取有关用户的信息(姓名、电子邮件等)。然后在整个系统中使用该数据,包括对角色进行身份验证。这是用户类:

class User{

protected $user_id; 
protected $session_hash;

protected $user_username;
protected $user_email;
protected $user_role_id;
protected $user_role_name;

protected $user_first_name;
protected $user_last_name;

public function __construct($user_id, $session_hash){
    $this->user_id = $user_id;
    $this->session_hash = $session_hash;
}   

public function __get($name){
    return $this->name; 
}

public function __set($name, $value){
    $this->$name = $value;
}

public function getLoggedUserInfo(){
    global $db;
    $query = new Query($db->link);

    if($user_matches = $query->select("SELECT name, mail FROM ".TABLE_PREFIX.".d7_users WHERE uid = '".$this->user_id."'")){
        $this->user_username = $user_matches[0]['name'];
        $this->user_email = $user_matches[0]['mail'];

        $this->user_role_id = $this->getLoggedUserRoleId($this->user_id);
        $this->user_role_name = $this->getLoggedUserRoleName($this->user_role_id);
        $this->user_first_name = $this->getLoggedUserFirstName($this->user_id);
        $this->user_last_name = $this->getLoggedUserLastName($this->user_id);

        $user_information_arr = array(
                                'user_id' => $this->user_id,
                                'user_username' => $this->user_username,
                                'user_first_name' => $this->user_first_name,
                                'user_last_name' => $this->user_last_name,
                                'user_email' => $this->user_email,
                                'user_role_id' => $this->user_role_id,
                                'user_role_name' => $this->user_role_name,
                            );

        return $user_information_arr;
    } 
    return false;   
}

private function getLoggedUserRoleId($user_id){
    global $db;
    $query = new Query($db->link);

    if($role_id_matches = $query->select("SELECT rid FROM ".TABLE_PREFIX.".d7_users_roles WHERE uid= '".$user_id."'")){
        $this->user_role_id = $role_id_matches[0]['rid'];
        return $this->user_role_id;
    } 
    return false;   
}

private function getLoggedUserRoleName($role_id){
    global $db;
    $query = new Query($db->link);

    if($role_name_matches = $query->select("SELECT name FROM ".TABLE_PREFIX.".d7_role WHERE rid = '".$role_id."'")){
        return $role_name_matches[0]['name'];
    } 
    return false;   
}

private function getLoggedUserFirstName($user_id){
    global $db;
    $query = new Query($db->link);

    if($first_name_matches = $query->select("SELECT field_first_name_value FROM ".TABLE_PREFIX.".d7_field_revision_field_first_name WHERE entity_id='".$user_id."'")){
        return $first_name_matches[0]['field_first_name_value'];
    } 
    return false;       
}

private function getLoggedUserLastName($user_id){
    global $db;
    $query = new Query($db->link);

    if($last_name_matches = $query->select("SELECT field_last_name_value FROM ".TABLE_PREFIX.".d7_field_revision_field_last_name WHERE entity_id='".$user_id."'")){
        return $last_name_matches[0]['field_last_name_value'];
    } 
    return false;       
}

}

4

1 回答 1

0

将现有实例中的查询对象传递到 User 类的构造函数中。

protected $queryObject;

public function __construct($user_id, $session_hash, Query $query = NULL){
    $this->user_id = $user_id;
    $this->session_hash = $session_hash;
    $this->queryObject = $query;
}
于 2014-08-28T15:31:11.797 回答