PHP函数 - 忽略一些默认参数 [英] PHP function - ignore some default parameters

查看:104
本文介绍了PHP函数 - 忽略一些默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

任何方式来指定PHP中的可选参数值?




$ b $

如果我有这样的功能:



pre> public function getSomething($ orderBy ='x',$ direction ='DESC',$ limit = null){

//做一些随机的事情


$ / code>

调用函数时可以忽略前两个字段,例如:

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

我知道我可以传递第一个和第二个参数,但所有即时通讯问的是如果他们是某种特殊的关键字,只是说使用默认值。



希望这是有道理的。它不是一个问题,只是好奇而已。



感谢您的阅读

解决方案

你需要自己做。您可以使用 null 来表示应该使用默认值:

  public function getSomething($ orderBy = null,$ direction = null,$ limit = null){
// fallback
if($ orderBy === null)$ orderBy ='x';
if($ direction === null)$ direction ='DESC';

//做一些随意的事情
}

code> null 在调用它时表明您要使用默认值:

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






有时我使用的另一个可能的解决方案是额外的参数列表,包含所有可选参数:

  public function foo($ options = array()) {
//与默认值合并
$ options = array_merge(array(
'orderBy'=>'x',
'direction'=&'; DESC',
'limit'=> null
),$ options);

//做东西
}

不需要指定所有可选参数。 array_merge() 确保您始终处理完整选项集。你可以像这样使用它:

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

好像这个特殊情况下没有必要的参数,但是如果你需要的话,只需添加它在可选项之前:

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


Possible Duplicate:
Any way to specify optional parameter values in PHP?

just randomly came across this.

If I have a function like so:

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.

For example:

$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.

thanks for reading

解决方案

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
}

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
}

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函数 - 忽略一些默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆