OOP 设计问题 - 避免在类似的类中重复代码?

发布于 2024-10-06 23:06:05 字数 774 浏览 1 评论 0原文

我正在用 PHP 为我们正在开发的网站的服务器端部分编写一堆类。这些类看起来像这样:

class SomeEntity {
    // These fields are often different in different classes
    private $field1 = 0, $field2 = 0, ... ;

    // All of the classes have one of these
    static function create($field1, $field2) {
        // Do database stuff in here...
    }

    // All of the classes have similar constructors too
    function __construct($id_number) {
        // Do more database stuff in here...
    }

    // Various functions specific to this class
    // Some functions in common with other classes
}

问题是有很多这样的类,它们都需要有类似的构造函数和一些通用函数,所以我理想地希望编写一个超类来处理所有这些东西,这样就可以最小化复制/粘贴正在进行。然而,每个子类都有不同的实例变量和参数,那么设计超类的最佳方式是什么?

(也许更好地说,如何编写构造函数或其他函数来处理类的实例变量,但不一定知道类的实例变量是什么并按名称对它们进行硬编码?)

I'm writing a bunch of classes in PHP for the server-side portion of a website we're developing. The classes look something like this:

class SomeEntity {
    // These fields are often different in different classes
    private $field1 = 0, $field2 = 0, ... ;

    // All of the classes have one of these
    static function create($field1, $field2) {
        // Do database stuff in here...
    }

    // All of the classes have similar constructors too
    function __construct($id_number) {
        // Do more database stuff in here...
    }

    // Various functions specific to this class
    // Some functions in common with other classes
}

The issue is there are a lot of these classes and they all need to have similar constructors and a few common functions, so I'd ideally want to write a superclass to handle all this stuff so that there's minimal copying/pasting going on. However, each of the subclasses has different instance variables and parameters, so what would the best way to design the superclass be?

(To phrase it perhaps slightly better, how can write a constructor function or other functions that do stuff with the instance variables of the class but without necessarily knowing what the class' instance variables are and hard-coding them by name?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-10-13 23:06:05

您可以通过多种方式实现非常通用的“实体”类型类,尤其是您利用各种魔术方法。

考虑这样的类(只是一些类似实体的类共享的随机便捷方法):

<?php
abstract class AbstractEntity {

  protected $properties;

  public function setData($data){
    foreach($this->properties as $p){
        if (isset($data[$p])) $this->$p = $data[$p];
    }
  }

  public function toArray(){
    $array = array();
    foreach($this->properties as $p){
       $array[$p] = $this->$p;
       //some types of properties might get special handling
       if ($p instanceof DateTime){
           $array[$p] = $this->$p->format('Y-m-d H:i:s');
       }
    }
  }

  public function __set($pname,$pvalue){
     if (! in_array($pname,$this->properties)){
        throw new Exception("'$pname' is not a valid property!");
     }
     $this->$pname = $pvalue;
  }
}


<?php

class Person extends AbstractEntity {
   protected $properties = array('firstname','lastname','email','created','modified');
}

You can go quite a ways towards a very generic "Entity" type class, especially is you leverage the various magic methods.

Consider class like this (just some random convenience methods for entity-like classes to share):

<?php
abstract class AbstractEntity {

  protected $properties;

  public function setData($data){
    foreach($this->properties as $p){
        if (isset($data[$p])) $this->$p = $data[$p];
    }
  }

  public function toArray(){
    $array = array();
    foreach($this->properties as $p){
       $array[$p] = $this->$p;
       //some types of properties might get special handling
       if ($p instanceof DateTime){
           $array[$p] = $this->$p->format('Y-m-d H:i:s');
       }
    }
  }

  public function __set($pname,$pvalue){
     if (! in_array($pname,$this->properties)){
        throw new Exception("'$pname' is not a valid property!");
     }
     $this->$pname = $pvalue;
  }
}


<?php

class Person extends AbstractEntity {
   protected $properties = array('firstname','lastname','email','created','modified');
}
蹲墙角沉默 2024-10-13 23:06:05

基本上,您可以将重复的任何内容分离到父类或辅助类中。

如果它是与该对象相关的常见活动,并且适用于类似的对象,则可以将其放入父类中并从中继承。如果该父级的子级具有相似的成员/属性,但由于某种原因而命名不同,您只需编写方法来接受参数,然后在对该方法的调用中传递不同的属性名称。

如果它是与该对象相关的常见活动,并且仅与该对象相关,那么它就成为需要它的子类中的方法。

如果它是与相关类无关的常见活动,那么您创建一个新类来管理与该活动相关的事物,并在该类中编写一个其他类可以调用的公共方法。

Basically, you separate anything you repeat into either a parent class, or a helper class.

If it's a common activity that relates to the object, and would apply to similar objects, you put that in a parent class and inherit from it. If the children of this parent have similar members/properties but are named differently for whatever reason, you just write the method to accept parameters then pass the different property names in the call to that method.

If it's a common activity that relates to the object, and only that object, it becomes a method in the child class which needs it.

If it's a common activity that doesn't relate to the class in question, then you create a new class that manages things relating to that activity, and write a public method in that class that your other classes can call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文