比开关盒更好的方法

A better way than a switch case

我有一个方法利用类似于以下代码块的 switch-case 块:

    switch ($totalMonthsFromImmigration) {
        case $totalMonthsFromImmigration <= 18:
            $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', null);
            break;
        case $totalMonthsFromImmigration <= 30:
            $this->clauses->put('immigrant_1', null);
            $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_3', null);
            break;
        case $totalMonthsFromImmigration <= 42:
            $this->clauses->put('immigrant_1', null);
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
            break;
    }

从上面的例子可以看出,case做的东西差不多,还是有点区别的。

有没有更优雅的方式来处理这种情况?我不太喜欢重复的代码。

尝试设置默认值,然后根据条件添加值

$this->clauses->put('immigrant_1', null);
$this->clauses->put('immigrant_2', null);
$this->clauses->put('immigrant_3', null);

switch ($totalMonthsFromImmigration) {
            case $totalMonthsFromImmigration <= 18:
                $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
                break;
            case $totalMonthsFromImmigration <= 30:
                $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
                break;
            case $totalMonthsFromImmigration <= 42:
                $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
                break;
 }

另一种方法,如果您想从 Switch case

更改,请尝试 IF
if ($totalMonthsFromImmigration <= 18){
 $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
} else if ($totalMonthsFromImmigration <= 30) {
 $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
else if ($totalMonthsFromImmigration <= 42) {
 $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
}

如果不需要像 @Jignesh Joisar 这样的默认值调用 setter 两次:

$this->clauses->put('immigrant_1', ($totalMonthsFromImmigration <= 18) ? $this->clauses->get('immigrant') : null);
$this->clauses->put('immigrant_2', ($totalMonthsFromImmigration > 18 && $totalMonthsFromImmigration <= 30) ? $this->clauses->get('immigrant') : null);
$this->clauses->put('immigrant_3', ($totalMonthsFromImmigration > 30 && $totalMonthsFromImmigration <= 42) ? $this->clauses->get('immigrant') : null);

我认为有一个数学公式可以代替使用 switch case 或 if else。但是我没有找到。这是基于我在这里看到的答案的解决方案:

    $this->clauses->put('immigrant_1', null);
    $this->clauses->put('immigrant_2', null);
    $this->clauses->put('immigrant_3', null);

    if ($totalMonthsFromImmigration <= 18) {
        $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
    } else if ($totalMonthsFromImmigration <= 30) {
        $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
    } else if ($totalMonthsFromImmigration <= 42) {
        $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
    }
switch ($totalMonthsFromImmigration) {
         case $totalMonthsFromImmigration <= 42:
            $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', null);
        break;
        default:
           echo "Your logic you want greeter then 42"

}