如何重构每年更改的php类以消除重复的代码?

我有一个用于创建FDF文件的类结构,该结构每年都会更改。这些类产生数组,并将这些数组转换为fdf字符串,但这并不重要。

我的第一堂课是Fdf13,它构成了完整的数组:

class Fdf13 extends FdfBaseStuff
{
      protected function fdfArray()                                
      {                                                     
          $this->buildPart1Dic();                                
          $this->buildPart1();                                

          $this->buildPart3();                                
          $this->buildPart4();                                
          $this->buildPart5();                                
          $this->buildPart6();                                
          $this->buildPart7();                                                         
          $this->buildPart8();                                                     
          $this->buildPart9();                                                                  
          $this->buildPart10();                                                                  
          $this->buildPart11();                                                      

          return $this->ret;                                                         
      }

      protected function buildPart5()
      {
          $this->ret = $this->add($this->ret,$this->calculator->lorem,'32');
          $this->ret = $this->add($this->ret,$this->calculator->ipsum,'33');
          $this->ret = $this->add($this->ret,$this->calculator->sit,'34');
          $this->ret = $this->add($this->ret,$this->calculator->amet,'35');
       }
...
}

第二年,我需要一门新课程,Fdf14。我必须在builsPart5中插入一行33a。我的Fdf14继承自Fdf13

class Fdf14 extends Fdf13
{
      protected function buildPart5()
      {
          $this->ret = $this->add($this->ret,$this->calculator->dolor,'33a'); // new item
          $this->ret = $this->add($this->ret,'35');
       }
...
}

我现在在Fdf14、15 ... 20中有一堆重复的行。 add方法只是将其返回值(字符串)添加到$ this-> ret数组中。

如何用最少的重复行做到这一点? 我曾想过简单地向子类中的ret数组添加新值,但是项的顺序很重要。 (不在fdf中,但我也有XML构建器),所以我不能只附加一个新值。

我的第二个想法是在新年课上创建一个'schema'数组,该数组具有正确的项目顺序,但具有空值,并使用buildPartX方法更新这些值,但是必须是一种更优雅的方法。

zhuangmisi 回答:如何重构每年更改的php类以消除重复的代码?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2682591.html

大家都在问