在PHP类方法中声明查询对象

时间:2014-08-27 20:46:24

标签: php database-abstraction

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

有没有办法不必每次都声明一个新对象?我觉得可能会在某些时候陷入服务器。

User类的作用是从数据库中提取有关用户的信息(名称,电子邮件等)。然后在整个系统中使用该数据,包括验证角色。这是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;       
}

}

1 个答案:

答案 0 :(得分: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;
}