【问题标题】:Declaring Query Objects in PHP Class Methods在 PHP 类方法中声明查询对象
【发布时间】:2014-10-21 14:21:17
【问题描述】:

我正在构建一个面向对象的表单系统。我正在整理一个包含大约八个函数的用户类。每个函数都包含一个 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;       
}

}

【问题讨论】:

  • 您可以将 Query 对象传递到 User 类的构造函数中,并将其存储为类的属性。在构造函数中实例化查询对象也是一种选择,但不太理想,因为您随后将耦合您的 User 类和 Query 类。这将有助于更全面地了解您的 User 类的角色。
  • 你所拥有的与 OOP 完全无关。
  • Drumbeg 我认为您的第一个解决方案在我只传递查询对象并将其存储为类属性的情况下效果最佳。我宁愿将 User 和 Query 类分开。谢谢!

标签: php database-abstraction


【解决方案1】:

将现有实例中的查询对象传递给 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;
}

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2016-12-13
    • 2023-03-20
    • 2023-02-04
    • 2014-12-01
    • 2017-07-20
    相关资源
    最近更新 更多