虫言虫语 虫言虫语

PHP整洁之道节选

in PHPread (227391) 文章转载请注明来源!

函数

函数参数最好少于2个

限制函数参数个数极其重要因为它是你函数测试容易点。有超过3个可选参数参数导致一个爆炸式组合增长,你会有成吨独立参数情形要测试。

无参数是理想情况。1个或2个都可以,最好避免3个。再多旧需要加固了。通常如果你的函数有超过两个参数,说明他多做了一些事。 在参数少的情况里,大多数时候一个高级别对象(数组)作为参数就足够应付。

Bad:

function createMenu($title, $body, $buttonText, $cancellable) {
  // ...
}

Good:

class menuConfig() {
  public $title;
  public $body;
  public $buttonText;
  public $cancellable = false;
}

$config = new MenuConfig();
$config->title = 'Foo';
$config->body = 'Bar';
$config->buttonText = 'Baz';
$config->cancellable = true;

function createMenu(MenuConfig $config) {
  // ...
}
jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

PHP
发表新评论 取消回复
已有 45662 条评论
  1. hsioabaxy
    hsioabaxy
    回复

    online viagra https://xvirgaronline.com/ - buy viagra viagra online viagra generic buy viagra

  2. hsioabaxy
    hsioabaxy
    回复

    cheap viagra https://xvirgaronline.com/ - viagra online viagra buy viagra cheap viagra

  3. Approved viagra

    Mmiovt stmtwe discount viagra cheap ed pills

  4. hsioabaxy
    hsioabaxy
    回复

    viagra https://xvirgaronline.com/ - viagra online viagra viagra generic viagra generic

  5. hsioabaxy
    hsioabaxy
    回复

    cheap viagra https://xvirgaronline.com/ - cheap viagra generic viagra viagra online buy viagra

  6. hsioabaxy
    hsioabaxy
    回复

    online viagra https://xvirgaronline.com/ - viagra viagra generic viagra viagra generic

  7. vrdcabaxy
    vrdcabaxy
    回复

    viagra generic https://sophiapharmsd.com/ - viagra sale viagra viagra viagra

  8. vrdcabaxy
    vrdcabaxy
    回复

    viagra for sale https://sophiapharmsd.com/ - viagra for sale viagra sale viagra generic viagra for sale

  9. hsioabaxy
    hsioabaxy
    回复

    viagra online https://xvirgaronline.com/ - viagra online viagra viagra online viagra

  10. hsioabaxy
    hsioabaxy
    回复

    online viagra https://xvirgaronline.com/ - viagra online viagra viagra online viagra

  11. Amitsflormror
    Amitsflormror
    回复

    cbd oils buy cbd oil best cbd oil cdb oils http://buycbdoilonline.us.com/ - cbd gummies

  12. swimarumTauff
    swimarumTauff
    回复

    http://cbdcreamwww.com/ cbd products cannabis oil buy cbd oil cbd

  13. vrdcabaxy
    vrdcabaxy
    回复

    viagra https://sophiapharmsd.com/ - viagra sale viagra for sale viagra for sale viagra for sale

  14. smagmevagype
    smagmevagype
    回复

    http://cbd-store.us.com/ cbd oils pure cbd oil cbd oil for sale cbd oil

  15. vrdcabaxy
    vrdcabaxy
    回复

    viagra sale https://sophiapharmsd.com/ - viagra for sale viagra generic viagra viagra for sale

  16. swimarumTauff
    swimarumTauff
    回复

    buy cbd buy hemp oil hemp cbd oil buy hemp oil

  17. hsioabaxy
    hsioabaxy
    回复

    buy viagra https://xvirgaronline.com/ - viagra viagra online viagra generic cheap viagra

  18. vrdcabaxy
    vrdcabaxy
    回复

    viagra for sale https://sophiapharmsd.com/ - viagra generic viagra for sale viagra sale viagra generic

  19. smagmevagype
    smagmevagype
    回复

    cbd oil for dogs buy hemp oil cbd oil for dogs cbd products http://cbd-store.us.com/ - cbd drops

  20. crapyragacoup
    crapyragacoup
    回复

    cbd for sale cbd drops cbd oil for dogs cbd cream http://cbdcreamww.com/ - best cbd oil buy cbd oil

博客已萌萌哒运行
© 2021 由 Typecho 强力驱动.Theme by Yodu
前篇 后篇
雷姆
拉姆