我编写了一个可以接受10个参数的PHP函数,但只需要2个.有时,我想定义第八个参数,但我不想为每个参数输入空字符串,直到我达到第八个.
我的一个想法是传递带有参数数组的抽象函数,这些参数将它传递给实际函数.
有没有更好的方法来设置功能,所以我只能传入我想要的参数?
在PHP 4/5中是否可以在调用时指定一个命名的可选参数,跳过你不想指定的参数(比如在python中)?
就像是:
function foo($a,$b='', $c='') {
// whatever
}
foo("hello", $c="bar"); // we want $b as the default, but specify $c
Run Code Online (Sandbox Code Playgroud)
谢谢
可能重复:
在PHP中指定可选参数值的任何方法?
随便碰到了这个.
如果我有这样的功能:
public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){
//do something random
}
Run Code Online (Sandbox Code Playgroud)
调用函数时,可以忽略前两个字段并保留默认值但指定第3个字段.
例如:
$random = $this->my_model->getSomething(USE_DEFAULT, USE_DEFAULT, 10);
Run Code Online (Sandbox Code Playgroud)
我知道我可以传递第一个和第二个参数,但我要问的是,它们是否是某种特殊的关键字,只是说使用默认值.
希望有道理.这不是问题,只是好奇.
谢谢阅读