喜迎
春节

PHP中if else的优化方案


前言:在项目开发中,经常会遇到多个复杂if else的情况,下面说说这种情况的优化方案。

提前return

让正常流程走主干,非正常流程提前return,去除不必要的else,适用于函数参数校验。

  • 优化前
    1
    2
    3
    4
    5
    if (condition){
    doSomething
    }else{
    if (condition){
    doSomething
    }else{
    return ;
    }
  • 优化后
    1
    2
    3
    4
    if (!condition){
    if (!condition){
    return ;
    }
    doSomething

使用三目运算符

适用于根据条件给变量赋值。

  • 优化前
    1
    2
    3
    4
    5
    if (condition){
    $status = 1;
    }else{
    $status = if (condition){
    $status = 1;
    }else{
    $status = 0;
    }
  • 优化后
    1
    $status = condition ? 1 : $status = condition ? 1 : 0;

合并条件表达式

  • 优化前
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if (condition1){
    return true;
    }

    if (condition2){
    return true;
    }else{
    return if (condition1){
    return true;
    }

    if (condition2){
    return true;
    }else{
    return false;
    }
  • 优化后
    1
    2
    3
    4
    5
    if (condition1 || condition2){
    return true;
    }else{
    return if (condition1 || condition2){
    return true;
    }else{
    return false;
    }

使用switch case优化

php8可以使用match表达式

  • 优化前
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    class A{
    public static function handle(string $act, int $userId){
    if ($act == 'sign in') {
    self::sign($userId);
    } elseif ($act == 'register user') {
    self::register($userId);
    } elseif ($act == 'change password') {
    self::changePwd($userId);
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int class A{
    public static function handle(string $act, int $userId){
    if ($act == 'sign in') {
    self::sign($userId);
    } elseif ($act == 'register user') {
    self::register($userId);
    } elseif ($act == 'change password') {
    self::changePwd($userId);
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int $userId){}

    }
  • 优化后
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class A{
    public static function handle(string $act, int $userId){
    switch ($act) {
    case 'sign in':
    self::sign($userId);
    break;
    case 'register user':
    self::register($userId);
    break;
    case 'change password':
    self::changePwd($userId);
    break;
    default:
    break;
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int class A{
    public static function handle(string $act, int $userId){
    switch ($act) {
    case 'sign in':
    self::sign($userId);
    break;
    case 'register user':
    self::register($userId);
    break;
    case 'change password':
    self::changePwd($userId);
    break;
    default:
    break;
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int $userId){}

    }
    效果:和原来的差不多,每次新增方法后还得修改代码。

使用枚举

  • 优化前
    1
    2
    3
    4
    5
    6
    7
    if ($status == 0){
    return '待支付';
    }elseif($status == 1){
    return '已支付';
    }else{
    return if ($status == 0){
    return '待支付';
    }elseif($status == 1){
    return '已支付';
    }else{
    return '支付失败';
    }
  • 优化后
    1
    2
    3
    4
    5
    6
    7
    $orderStatus = [
    0 => '待支付',
    1 => '已支付',
    2 => '支付失败',
    ];

    return $orderStatus[$orderStatus = [
    0 => '待支付',
    1 => '已支付',
    2 => '支付失败',
    ];

    return $orderStatus[$status];

表驱动法

  • 优化前
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    class A{
    public static function handle(string $act, int $userId){
    if ($act == 'sign in') {
    self::sign($userId);
    } elseif ($act == 'register user') {
    self::register($userId);
    } elseif ($act == 'change password') {
    self::changePwd($userId);
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int class A{
    public static function handle(string $act, int $userId){
    if ($act == 'sign in') {
    self::sign($userId);
    } elseif ($act == 'register user') {
    self::register($userId);
    } elseif ($act == 'change password') {
    self::changePwd($userId);
    }
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int $userId){}

    }
  • 优化后
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class A{
    private static $acts = [
    'sign in' => 'sign',
    'register use' => 'register',
    'change password' => 'changePwd',
    ];

    public static function handle(string $act, int $userId){
    $fun = !empty($act) && isset(self::$acts[$act]) ? self::$acts[$act] : null;
    if ($fun) call_user_func($fun, $userId);
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int class A{
    private static $acts = [
    'sign in' => 'sign',
    'register use' => 'register',
    'change password' => 'changePwd',
    ];

    public static function handle(string $act, int $userId){
    $fun = !empty($act) && isset(self::$acts[$act]) ? self::$acts[$act] : null;
    if ($fun) call_user_func($fun, $userId);
    }

    protected static function sign(int $userId){}

    protected static function register(int $userId){}

    protected static function changePwd(int $userId){}

    }
    效果:新增方法后,只需修改配置项,不用变更其他代码。

策略模式+工厂方法消除if-else

参考:https://www.phpmianshi.com/?id=197


文章作者: Crazy Boy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Crazy Boy !
评 论
 上一篇
Lua笔记
Lua笔记
进入交互式Lua:lua -i 退出交互式Lua: linux:ctrl+D windows:ctrl+z 或者 os.exit() Lua的数组下标是从1开始的
2022-11-13
下一篇 
Windows下如何将桌面位置移到其他地方
Windows下如何将桌面位置移到其他地方
一般windows系统的桌面都是放在C盘里的,如果桌面内容过多就会导致C盘空间不足。为了保证电脑的干净整洁,除了需要定期清理桌面内容,其实我们还可以把桌面移动到其他地方。 在”快速访问”里找到桌面 右键”属性”,在”位置”栏点击”移动”
2022-08-29
  目录