是否有可能在php像python有一个名为函数参数?一个示例用例是:
function foo($username = "", $password = "", $timeout = 10) {
}
Run Code Online (Sandbox Code Playgroud)
我想覆盖$timeout:
foo("", "", 3);
Run Code Online (Sandbox Code Playgroud)
丑陋.我宁愿这样做:
foo(timeout=3);
Run Code Online (Sandbox Code Playgroud) 在PHP中,您可以调用函数.通过使用这些语句不调用所有参数.
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的功能
test();
Run Code Online (Sandbox Code Playgroud)
所以我只想说我希望最后一个是不同的而不是其他的.我能做到的唯一方法是做到这一点,但没有成功:
test('test1','test2','hi i am different');
Run Code Online (Sandbox Code Playgroud)
我试过这个:
test(,,'hi i am different');
test(default,default,'hi i am different');
Run Code Online (Sandbox Code Playgroud)
做这样的事情的最佳方法是什么?
当尝试使用任意参数集调用子类中的函数时,我遇到以下问题:
class Base{
function callDerived($method,$params){
call_user_func_array(array($this,$method),$params);
}
}
class Derived extends Base{
function test($foo,$bar){
print "foo=$foo, bar=$bar\n";
}
}
$d = new Derived();
$d->callDerived('test',array('bar'=>'2','foo'=>1));
Run Code Online (Sandbox Code Playgroud)
输出:
foo=2, bar=1
Run Code Online (Sandbox Code Playgroud)
哪个...并不是我想要的 - 除了使用func_get_args的索引顺序重新组合数组之外,有没有办法实现这一目标?是的,当然,我可以简单地传递整个数组并在函数中处理它...但这不是我想要做的.
谢谢
我有这个:
function foo($a='apple', $b='brown', $c='Capulet') {
// do something
}
Run Code Online (Sandbox Code Playgroud)
这样的事情是可能的:
foo('aardvark', <use the default, please>, 'Montague');
Run Code Online (Sandbox Code Playgroud) 可能重复:
命名PHP可选参数?
我想做这个:
function they_said($alice = "", $bob = "", $charlie = "") {
echo "Alice said '$alice'";
echo "Bob said '$bob'";
echo "Charlie said '$charlie'";
}
they_said($charlie => "Where are the cookies!?");
Run Code Online (Sandbox Code Playgroud)
这样我就可以忽略传递前两个参数并简单地传递我想要的那个.
这是python中的一个例子.
在PHP中是否有一种方法可以使用在其声明中具有可选参数的函数,其中我不必传递已经声明了值的可选参数,并且只传递具有不同值的下一个参数.参数列表.
假设我有一个有4个参数的函数,2个强制,2个可选.我不想为可选参数使用空值.在使用中,有我想要使用的功能和值3的情况下RD说法是一样的默认值,但4的值个参数是不同的.
我正在寻找一个不那么详细的解决方案,它允许我只传递与默认值不同的参数而不考虑函数声明中的顺序.
createUrl($host, $path, $protocol='http', $port = 80) {
//doSomething
return $protocol.'://'.$host.':'.$port.'/'.$path;
}
Run Code Online (Sandbox Code Playgroud)
我发现自己重复声明变量,以便我可以使用函数即使用$ port,我使用函数作用域外的默认值重新声明$ protocol即
$protocol = "http";
$port = 8080;
Run Code Online (Sandbox Code Playgroud)
有没有办法传递第二个可选参数($ port)而不传递$ protocol,它会"自动"填写$ protocol的默认值ie
getHttpUrl($server, $path, $port);
Run Code Online (Sandbox Code Playgroud)
这在某些语言中是可能的,例如命名可选参数形式的Dart.请参阅此SO线程中的用法.他们是PHP中的类似解决方案
如何在 PHP 7+ 中实现命名参数功能?
理想的语法是:
find($wildcard, relative = true, listIfEmpty = false) {
...
}
Run Code Online (Sandbox Code Playgroud)
但是没有这样的解决方案。在答案中,我实现了最短的解决方案,支持:
有关更多信息和实施,请参阅答案
很长一段时间我都在研究php.
但是我可能不知道一个问题
就像我有一个功能如下:
function hello($param1, $param2="2", $param3="3", $param4="4")
Run Code Online (Sandbox Code Playgroud)
现在每当我使用这个函数,如果我需要第4个参数那个$ param4那么我仍然需要像这样将所有空白称为空白:
hello(1, '', '', "param4");
Run Code Online (Sandbox Code Playgroud)
那么还有另一种方法可以在调用中传递第1和第4个参数而不是长列表空格吗?
或者还有其他标准方法吗?
假设有一个带有一些参数的函数,并且我有一个关联数组(或一个具有公共属性的简单对象 - 这几乎是相同的,因为我总是可以使用类型转换(object)$array),其键对应于函数参数名称及其值对应于函数调用参数。我如何调用它并将它们传递到那里?
<?php\nfunction f($b, $a) { echo "$a$b"; }\n// notice that the order of args may differ.\n$args = ['a' => 1, 'b' => 2];\ncall_user_func_array('f', $args); // expected output: 12 ; actual output: 21\nf($args); // expected output: 12 ; actual output: \xe2\x86\x93\n// Fatal error: Uncaught ArgumentCountError:\n// Too few arguments to function f(), 1 passed\nRun Code Online (Sandbox Code Playgroud)\n