我应该扩展这个类还是只使用它?

时间:2022-10-03 15:25:26

I am writing helper classes for a large project in PHP and I have written a class called Command. It is essentially an OOP wrapper around running system commands from PHP in a controlled way. It has methods like addOption() to add -a type options and addArgument() to add other arguments.

我正在为PHP中的大型项目编写辅助类,我编写了一个名为Command的类。它本质上是一个以受控方式从PHP运行系统命令的OOP包装器。它有像addOption()这样的方法来添加-a类型选项和addArgument()来添加其他参数。

I will need to do a lot of scp'ing of files around so I will be using the Command class a lot. The calls to scp are very specific in that I need certain options used every time.

我需要做很多scp'ing文件,所以我将使用Command类。对scp的调用非常具体,因为我每次都需要使用某些选项。

Does it make sense to extend this class and create a ScpCommand class? It seems to have an 'is a' relationship, but it is really just the same thing as the Command class with specific options every time. I don't really think I would need to override any methods with the exception of the constructor. I probably would add just class variables. It would really just be a convenience.

扩展此类并创建ScpCommand类是否有意义?它似乎有一个'is a'关系,但它实际上与Command类一样,每次都有特定的选项。我真的不认为我需要覆盖除构造函数之外的任何方法。我可能只会添加类变量。这真的只是一种便利。

What makes the most sense here?

最有意义的是什么?

1 个解决方案

#1


2  

If it is just configuration, why not consider a factory which returns a Command after doing your boiler-plate configuration for you?

如果只是配置,为什么不考虑在为您进行锅炉板配置后返回Command的工厂?

function getScpCommand($???) 
{
    $Command = new Command();
    $Command->addOption(/* scp specific this */);
    $Command->addArgument(/* scp specific that */);
    /* etc */
    $Command->addOption(/* handle your getScpCommand parameters here */)

    return $Command;
}

#1


2  

If it is just configuration, why not consider a factory which returns a Command after doing your boiler-plate configuration for you?

如果只是配置,为什么不考虑在为您进行锅炉板配置后返回Command的工厂?

function getScpCommand($???) 
{
    $Command = new Command();
    $Command->addOption(/* scp specific this */);
    $Command->addArgument(/* scp specific that */);
    /* etc */
    $Command->addOption(/* handle your getScpCommand parameters here */)

    return $Command;
}