是否可以跳过php(5)函数调用中具有默认值的参数?
我有这个:是否可以跳过php(5)函数调用中具有默认值的参数?,php,Php,我有这个: function foo($a='apple', $b='brown', $c='Capulet') { // do something } 这样的事情是否可能: foo('aardvark', <use the default, please>, 'Montague'); foo('aardvark','Montague'); 发现了这一点,这可能仍然是正确的: 简短回答:没有 长答案:是的,在上面概述的各种困难的方式中。你基本上找到了答案,但学术/高级方
function foo($a='apple', $b='brown', $c='Capulet') {
// do something
}
这样的事情是否可能:
foo('aardvark', <use the default, please>, 'Montague');
foo('aardvark','Montague');
发现了这一点,这可能仍然是正确的:
简短回答:没有
长答案:是的,在上面概述的各种困难的方式中。你基本上找到了答案,但学术/高级方法确实是我从未发现过的有用方法,但知道它的存在是有用的。如果它是你自己的函数而不是PHP的核心,你可以做:
function foo($arguments = []) {
$defaults = [
'an_argument' => 'a value',
'another_argument' => 'another value',
'third_argument' => 'yet another value!',
];
$arguments = array_merge($defaults, $arguments);
// now, do stuff!
}
foo(['another_argument' => 'not the default value!']);
如果它是您的函数,您可以使用
null
作为通配符,并在函数内部稍后设置默认值:
function foo($a=null, $b=null, $c=null) {
if (is_null($a)) {
$a = 'apple';
}
if (is_null($b)) {
$b = 'brown';
}
if (is_null($c)) {
$c = 'Capulet';
}
echo "$a, $b, $c";
}
然后您可以使用null
跳过它们:
foo('aardvark', null, 'Montague');
// output: "aarkvark, brown, Montague"
您可以使用一些怪癖,或者像建议的那样以数组的形式传递所有参数,或者使用一些解析func_get_args()并与默认值列表合并的过于复杂的代码。不复制粘贴它,你必须使用对象和特征。最后,为了能够传递所有类型的值(通过使它们成为默认参数替换的信号,不排除null或false),您必须声明一个伪特殊类型DefaultParam。 另一个缺点是,如果希望在任何IDE中获得类型提示或帮助,则必须复制函数声明中的名称和默认值
class DefaultParam {}
trait multi_arg_functions
{
private static function multi_arg($defaults, $list, $preserve_index = false)
{
$arg_keys = array_slice(array_keys($defaults), 0, count($list));
if ($preserve_index) {
$listed_arguments = array_slice($list, 0, count($arg_keys));
$extras = array_slice($list, count($arg_keys), null, true);
} else {
$listed_arguments = array_splice($list, 0, count($arg_keys));
$extras = &$list;
}
unset($list);
$arguments = array_combine($arg_keys, $listed_arguments);
$arguments = array_filter($arguments, function ($entry) {
return !($entry instanceof DefaultParam); //remove entries that mean default, a special class in this case
});
$arguments = array_merge($defaults, $arguments);
return [$arguments, $extras];
}
}
class b {
use multi_arg_functions;
static function func1($an_argument = 'a value', $another_argument = 'another value', $third_argument = 'yet another value') { //give defaults here to get hints in an IDE
list($args, $extras) = self::multi_arg( //note: duplicate names and defaults
[
'an_argument' => 'a value',
'another_argument' => 'another value',
'third_argument' => 'yet another value!',
], func_get_args());
echo json_encode(['args' => $args, 'extras' => $extras])."\n";
}
}
$default_param = new DefaultParam();
b::func1('value 1');
b::func1('value 2', $default_param, 'third argument');
b::func1('value 3', $default_param, 'third argument', 'fourth argument');
注意:通过使用preserve_index=true,您可以获得从原始索引开始的额外参数。从PHP 8开始,使用:
这使您可以根据需要绕过任意多个参数,允许它们采用默认值。这对于冗长的函数很有帮助,例如:
请注意,命名参数要求传递所有非可选参数。这些参数可以按位置传递(正如我在这里所做的,上面没有名称),也可以按任何顺序传递名称。我认为Curry对默认参数值没有任何作用-它们几乎是相互排斥的概念。有人建议在php5.5中实现这一功能,那么语法呢?
function foo($a='apple', $b='brown', $c='Capulet') {
// do something
}
foo('apple', c:'Montague');
setcookie('macadamia', httponly:true); // skips over 5 parameters