在PHP中命名参数(Named Arguments in PHP)
在C#中,有一个名为Named Arguments的4.0新功能,与Optional Parameters相得益彰。
private static void writeSomething(int a = 1, int b = 2){
// do something here;
}
static void Main()
{
writeSomething(b:3); // works pretty well
}
我正在使用此选项从用户获取一些设置值。
在PHP中,除了可选参数之外我找不到任何类似的东西,但我接受做$.fn.extend (jQuery)类函数:
function settings($options)
{
$defaults = array("name"=>"something","lastname"=>"else");
$settings = array_merge($defaults,$options);
}
settigs(array("lastname"=>"John");
我想知道你正在使用什么样的解决方案,或者你会用于相同的情况。
In C#, there is a new feature coming with 4.0 called Named Arguments and get along well with Optional Parameters.
private static void writeSomething(int a = 1, int b = 2){
// do something here;
}
static void Main()
{
writeSomething(b:3); // works pretty well
}
I was using this option to get some settings value from users.
In PHP, I cannot find anything similar except for the optional parameters but I am accepting doing $.fn.extend (jQuery) kind of function :
function settings($options)
{
$defaults = array("name"=>"something","lastname"=>"else");
$settings = array_merge($defaults,$options);
}
settigs(array("lastname"=>"John");
I am wondering what kind of solutions you are using or you would use for the same situation.
原文:https://stackoverflow.com/questions/6800379
更新时间:2020-01-05 08:06
最满意答案
正如您所知,PHP中不存在命名参数。
但是一种可能的解决方案是使用一个数组作为唯一参数 - 因为数组项可以命名为:
my_function(array(
'my_param' => 10,
'other_param' => 'hello, world!',
));
并且,在您的函数中,您将从该唯一数组参数中读取数据:
function my_function(array $params) {
// test if $params['my_param'] is set ; and use it if it is
// test if $params['other_param'] is set ; and use it if it is
// test if $params['yet_another_param'] is set ; and use it if it is
// ...
}
不过,这个想法有一个主要的不便之处:看看你的函数的定义,人们不知道它期望/他们可以通过什么参数。
他们每次想要调用你的功能时都必须阅读文档 - 这不是人们喜欢做的事情,是吗?
附加说明:IDE也无法提供提示; 和phpdoc也将被打破......
As you found out, named arguments don't exist in PHP.
But one possible solution would be to use one array as unique parameter -- as array items can be named :
my_function(array(
'my_param' => 10,
'other_param' => 'hello, world!',
));
And, in your function, you'd read data from that unique array parameter :
function my_function(array $params) {
// test if $params['my_param'] is set ; and use it if it is
// test if $params['other_param'] is set ; and use it if it is
// test if $params['yet_another_param'] is set ; and use it if it is
// ...
}
Still, there is one major inconvenient with this idea : looking at your function's definition, people will have no idea what parameters it expects / they can pass.
They will have to go read the documentation each time they want to call your function -- which is not something one loves to do, is it ?
Additionnal note : IDEs won't be able to provide hints either ; and phpdoc will be broken too...
2011-07-23
相关问答
尝试: $args = explode(",", key($_GET));
Try: $args = explode(",", key($_GET));
很像手册,在你的参数定义中使用一个equals( = )符号: function dosomething($var1, $var2, $var3 = 'somevalue'){
// Rest of function here...
}
Much like the manual, use an equals (=) sign in your definition of the parameters: function dosomething($var1, $var2, $var3 = '
...
header('Location: ...script.php?arg1=' . $string1 . '&arg2=' . $string2);
header('Location: ...script.php?arg1=' . $string1 . '&arg2=' . $string2);
正如您所知,PHP中不存在命名参数。 但是一种可能的解决方案是使用一个数组作为唯一参数 - 因为数组项可以命名为: my_function(array(
'my_param' => 10,
'other_param' => 'hello, world!',
));
并且,在您的函数中,您将从该唯一数组参数中读取数据: function my_function(array $params) {
// test if $params['my_param'] is set
...
我认为从错误的观点来看,如果您没有任何安全性,这是一个糟糕的主意。 使用地图是一种更安全的模拟方法。 您也可以在本地范围内设置这些变量的值,这可能是也可能不是问题。 calculateArea( $height = 5, $width = 10 ); # oops!
function calculateArea( $width, $height ) {
// do calculations here...
}
有了一个数组(地图),不管你把它放在什么位置。 calculateArea(
...
您可以声明参数的默认值: function addDate($years, $months = 0, $days = 0)
这样你不需要指定那些或者像'addDate(2,0,0)'那样调用你的函数 参见http://php.net/manual/functions.arguments.php You can declare default values for parameter: function addDate($years, $months = 0, $days = 0)
This w
...
命名参数不必按顺序排列。 class xyz:
def __init__ (self, a='1', b='2'):
print a,b
xyz(b=3,a=4)
xyz(a=5,b=6)
>>4 3
>>5 6
Named arguments don't have to be in order. class xyz:
def __init__ (self, a='1', b='2'):
print a,b
xyz(b=3,a=4)
xyz(
...
由于PHP不理解命名参数的概念(至少不是VB / A的方式),因此您必须尊重方法签名并以正确的顺序传递参数。 VB命名参数的存在只是为了允许您以错误的顺序传递参数,但该方法仍然具有已定义的签名,并且您可以传递未命名的参数,只要它们的顺序正确即可。 假设签名与此处定义的签名相同,我认为这应该有效: $com->SaveAs('mynewfilename.doc', 2, NULL, NULL, FALSE);
免责声明:我假设PHP COM将合理地处理NULL和FALSE - 这可能是一个愚蠢的假
...
您的实施有点不完整。 'cube'($x)是有效的PHP,但实际上并不是在PHP中调用函数的好方法。 (它甚至可以工作也很可怕。) 通过使用call_user_func ,您可以成功应用任何可调用的 function sum($term, $a, $next, $b) {
if ($a > $b)
return 0;
else
return call_user_func($term, $a)
+ sum($term, call_user_func($next,
...
解决方案将如下所示: 创建一个空的$param数组,并使用for循环遍历变量数组$variables 。 在循环的每次迭代中,将元素推送到$variables数组作为参考。 使用array_unshift()函数将$datatype推送到$variables数组的开头。 最后,使用call_user_func_array()函数来绑定每个参数。 所以你的myFetch()函数应该是这样的: function myFetch($query, $datatype, $variables){
i
...