• <tfoot id='nklG8'></tfoot>
    • <bdo id='nklG8'></bdo><ul id='nklG8'></ul>

    <small id='nklG8'></small><noframes id='nklG8'>

    <legend id='nklG8'><style id='nklG8'><dir id='nklG8'><q id='nklG8'></q></dir></style></legend>

      1. <i id='nklG8'><tr id='nklG8'><dt id='nklG8'><q id='nklG8'><span id='nklG8'><b id='nklG8'><form id='nklG8'><ins id='nklG8'></ins><ul id='nklG8'></ul><sub id='nklG8'></sub></form><legend id='nklG8'></legend><bdo id='nklG8'><pre id='nklG8'><center id='nklG8'></center></pre></bdo></b><th id='nklG8'></th></span></q></dt></tr></i><div id='nklG8'><tfoot id='nklG8'></tfoot><dl id='nklG8'><fieldset id='nklG8'></fieldset></dl></div>

        PHP 函数 - 忽略一些默认参数

        PHP function - ignore some default parameters(PHP 函数 - 忽略一些默认参数)

          <tfoot id='buZOp'></tfoot>
            <tbody id='buZOp'></tbody>

              <small id='buZOp'></small><noframes id='buZOp'>

            • <i id='buZOp'><tr id='buZOp'><dt id='buZOp'><q id='buZOp'><span id='buZOp'><b id='buZOp'><form id='buZOp'><ins id='buZOp'></ins><ul id='buZOp'></ul><sub id='buZOp'></sub></form><legend id='buZOp'></legend><bdo id='buZOp'><pre id='buZOp'><center id='buZOp'></center></pre></bdo></b><th id='buZOp'></th></span></q></dt></tr></i><div id='buZOp'><tfoot id='buZOp'></tfoot><dl id='buZOp'><fieldset id='buZOp'></fieldset></dl></div>

                <legend id='buZOp'><style id='buZOp'><dir id='buZOp'><q id='buZOp'></q></dir></style></legend>
                  <bdo id='buZOp'></bdo><ul id='buZOp'></ul>
                • 本文介绍了PHP 函数 - 忽略一些默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  有什么方法可以在 PHP 中指定可选参数值?

                  偶然发现了这个.

                  如果我有这样的功能:

                  public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){
                  
                  //do something random
                  
                  }
                  

                  调用该函数时,是否可以忽略前两个字段并将它们保留为默认值,但指定第三个字段.

                  When calling the function is it possible to ignore the first two fields and leave them default yet specify the 3rd.

                  例如:

                  $random = $this->my_model->getSomething(USE_DEFAULT, USE_DEFAULT, 10);
                  

                  我知道我可以传递第一个和第二个参数,但我只是问它们是否是某种特殊的关键字,只是说使用默认值.

                  I know I can pass the 1st and 2nd parameters but all im asking is if their is some kind of special keyword that just says use the default value.

                  希望这是有道理的.这不是问题,只是好奇.

                  Hope that makes sense. its not a problem, just curious.

                  感谢阅读

                  推荐答案

                  你需要自己做.您可以使用 null 来指示应使用默认值:

                  You need to do that yourself. You can use null to indicate that a default value should be used:

                  public function getSomething($orderBy = null, $direction = null, $limit = null) {
                      // fallbacks
                      if ($orderBy === null) $orderBy = 'x';
                      if ($direction === null) $direction = 'DESC';
                  
                      // do something random
                  }
                  

                  然后在调用它时传递null以表明您要使用默认值:

                  Then pass null when calling it to indicate that you want to use the defaults:

                  $random = $this->my_model->getSomething(null, null, 10);
                  

                  <小时>

                  我有时使用的另一种可能的解决方案是在参数列表的最后添加一个附加参数,包含所有可选参数:


                  Another possible solution that I use sometimes is an additional parameter at the very end of the parameter list, containing all optional parameters:

                  public function foo($options = array()) {
                      // merge with defaults
                      $options = array_merge(array(
                          'orderBy'   => 'x',
                          'direction' => 'DESC',
                          'limit'     => null
                      ), $options);
                  
                      // do stuff
                  }
                  

                  这样您就不需要指定所有可选参数.array_merge() 确保您始终处理一整套选项.你会像这样使用它:

                  That way you do not need to specify all optional arguments. array_merge() ensures that you are always dealing with a complete set of options. You would use it like this:

                  $random = $this->my_model->foo(array('limit' => 10));
                  

                  在这种特殊情况下似乎没有必需的参数,但如果您需要一个,只需将其添加到可选参数的前面即可:

                  It seems like there is no required parameter this particular case, but if you need one, simply add it in front of the optional ones:

                  public function foo($someRequiredParameter, $someOtherRequiredParameter, $options = array()) {
                      // ...
                  }
                  

                  这篇关于PHP 函数 - 忽略一些默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    <i id='E64FU'><tr id='E64FU'><dt id='E64FU'><q id='E64FU'><span id='E64FU'><b id='E64FU'><form id='E64FU'><ins id='E64FU'></ins><ul id='E64FU'></ul><sub id='E64FU'></sub></form><legend id='E64FU'></legend><bdo id='E64FU'><pre id='E64FU'><center id='E64FU'></center></pre></bdo></b><th id='E64FU'></th></span></q></dt></tr></i><div id='E64FU'><tfoot id='E64FU'></tfoot><dl id='E64FU'><fieldset id='E64FU'></fieldset></dl></div>
                      <tbody id='E64FU'></tbody>
                  • <small id='E64FU'></small><noframes id='E64FU'>

                    <tfoot id='E64FU'></tfoot>

                        <legend id='E64FU'><style id='E64FU'><dir id='E64FU'><q id='E64FU'></q></dir></style></legend>
                          <bdo id='E64FU'></bdo><ul id='E64FU'></ul>