如何删除此PHP代码中的代码重复?

如何删除此PHP代码中的代码重复?,php,code-duplication,Php,Code Duplication,有没有一种好方法可以消除此代码中的重复: if ($design->unit_system=="English") { define('LIMIT_AF', '200'); define('LIMIT_BF', '0'); define('LIMIT_CM', '50'); define('LIMIT_DB', '50'); define(

有没有一种好方法可以消除此代码中的重复:

        if ($design->unit_system=="English")
        {
            define('LIMIT_AF', '200');
            define('LIMIT_BF', '0');
            define('LIMIT_CM', '50');
            define('LIMIT_DB', '50');
            define('LIMIT_FB', '100');                            
        }
        else if ($design->unit_system=="Metric")
        {
            define('LIMIT_AF', convert(200, 'gpm', 'm3h'));
            define('LIMIT_BF', convert(0, 'gpm', 'm3h'));
            define('LIMIT_CM', convert(50, 'gpm', 'm3h'));
            define('LIMIT_DB', convert(50, 'psi', 'bar'));
            define('LIMIT_FB', convert(100, 'psi', 'bar'));
        }

我正在努力,是否任何努力都会比我现在所做的更加复杂。

这是怎么回事-根据OP编辑更新:

$constants = array(
    'LIMIT_AF' => array(200, 'gpm', 'm3h'),
    'LIMIT_BF' => array(0, 'gpm', 'm3h'),
    'LIMIT_CM' => array(50, 'gpm', 'm3h'),
    'LIMIT_DB' => array(50, 'psi', 'bar'),
    'LIMIT_FB' => array(100, 'psi', 'bar'),
);

foreach($constants as $key => $info){
    if($design->unit_system=="English"){
        define($key, $info[0]);
    }elseif($design->unit_system=="Metric"){
        define($key, convert($info[0], $info[1], $info[2]));
    }
}
这对我来说是“配置文件”

或者,您可以将值放入数组中,然后在执行以下操作后对其进行处理:

$settings=array('LIMIT_AF'=>200,...);
foreach ($settings as $name=>$value) {
    if (design->unit_system=="English") {
        define($name,$value);
    } else if ($design->unit_system=="Metric") {
        define($name,convert($value,'gpm','m3h'));
    } else {
        ## DONT FORGET TO THROW AN EXCEPTION rather than have these undefined!
    }   
}

在我看来,这已经足够清楚了。这很好,但可能偶尔会有不同的物理量——更新了这个问题……我想可能是当我有更多的单位系统时,但到目前为止,我还没有看到这种情况发生在现在实施它。顺便说一句,我花了一分钟的时间才弄明白为什么
info[1]
不会解析——不
$
!抱歉,从我的手机中键入:)