【问题标题】:An user class, good approach?一个用户类,好方法?
【发布时间】:2011-01-07 13:35:44
【问题描述】:

我正在构建一个用户类来管理通用用户的创建、删除和修改。我的课应该这样使用:

# creation
user::create($username, $password, $email); // Does not need of $id

# modification
$u = new user($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

基本上,该类包含一个静态方法 create(),它显然不需要使用的 id 作为参数。创建后,您可以收集用户信息并管理创建类用户实例的用户,并将用户的 $id 设置为参数。 这是一个好的设计还是我应该创建类似的东西:

# creation
$users = new genericUserMethod();
$users->create($username, $password, $email);

# modification
$u = new specificUser($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

...创建 2 个不同的类。或者有没有其他办法?

【问题讨论】:

    标签: php design-patterns


    【解决方案1】:

    两种流行的处理方法是Active Record 和Data mapper。 Doctrine 1 使用 Active Record 模式,而 Doctrine 2 使用 Data Mapper。简而言之:
    - 有了活动记录,你就有了同时处理数据和持久性的类
    - 使用 Data Mapper 你有数据类和处理持久性的类

    还有Data Access Object 模式可以在上述任何一个之上。

    您的第一个示例看起来像用于构建记录对象的不合理静态速记的活动记录模式(为什么没有多个构造函数或可选的 id - 新的为 null,现有的为整数)。

    第二个例子看起来像 DAO 在活动记录之上,看起来更普通。

    【讨论】:

      【解决方案2】:

      这可能是一种方法:

      class User {
          private $id;
          private $name;
          //more fields here
      
          public function __construct($id = null) {
              $this->id = $id;
              if(!is_null($this->id)) {
                  $this->load_user_data();
              }
          }
      
          protected function load_user_data() {
              //select from DB where id = $this->id and populate fields
          }
      
          public function save() {
              //if $this->id is null insert the user details in DB and populate $this->id with new user's id
              //else update DB with field (optionally check what has changed and update only if necessary)
          }
      
          public function delete() {
              //delete user if $this->id is not null
          }
      
          //fields getters and setters here as needed
      
      }
      

      使用示例:

      $mary = new User(); //fresh new user
      echo $mary->getId(); //returns null as this user is not inserted.
      $mary->setName('mary');
      $mary->save(); //insert user with name mary in the DB
      echo $mary->getId(); // returns an id as this user is now inserted
      
      $john = new User(2); // we assume there was a user john in DB with id = 2
      echo $john->getName(); //echoes 'john' if this was his name in DB
      

      您甚至可以在 getActiveUsers() 之类的类中定义静态方法,例如返回一个包含活动用户的数组...

      注意:这适用于非常简单的需求,如果您需要做复杂的事情,我建议您使用 ORM 库,如@What is the question

      【讨论】:

      • 我也喜欢这样,这也是我过去所做的。也许添加一些使用(创建新用户、加载旧用户、删除、编辑等)...除此之外,+1
      • 使用构造函数从数据库中隐式加载对象并不是“好的设计”。您不希望有可能表明给定的 $id 不存在于数据库中。此外,构造函数应仅用于创建类的新有效实例。 -1
      • @mschneider 你是对的,构造函数也可以设置为受保护的,并制作 2 个静态方法“create”和“load”,例如在内部调用构造函数......但这只是作为一个简单的骨架...
      • @mschneider 以什么方式使用构造函数加载数据是不好的设计?我在大学里睡过头了怎么办?:)
      • @Charlie:这称为活动记录模式。查一下。
      【解决方案3】:

      第一个。也许您应该查看 ActiveRecord/ActiveModel 以获得更多灵感。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-17
        • 2012-07-20
        • 1970-01-01
        • 1970-01-01
        • 2018-02-15
        • 2013-07-21
        • 2021-10-23
        • 1970-01-01
        相关资源
        最近更新 更多