参数多于预期:PHP 中没有错误?
More parameter than expected: no error in PHP?
请查看这段代码,我在其中向自定义函数传递了比预期更多的参数:
error_reporting(E_ALL);
ini_set("display_errors", 1);
daidai("asassa", "asgfasfas", "asassa");
function daidai($aa)
{
echo $aa;
}
这根本没有发出错误,而我期待 Warning: function daidai() expects at most 1 parameter, 3 given
令我困惑的是,这会按预期发出上述错误:
$Odate=new DateTime();
$sfasfasf=$Odate->setTime("23", "59", "30", "unexpected");
为什么?
那是因为setTime()
方法本身会对参数个数进行检查,如果你想在你的函数中使用它,你必须自己实现。
作为参考,您可以查看 the docs for func_num_args()
, func_get_arg()
and func_get_args()
来自文档:
No special syntax is required to note that a function is variadic;
however access to the function's arguments must use func_num_args(),
func_get_arg() and func_get_args().
因此,php 确实将每个函数都视为可变函数。
请查看这段代码,我在其中向自定义函数传递了比预期更多的参数:
error_reporting(E_ALL);
ini_set("display_errors", 1);
daidai("asassa", "asgfasfas", "asassa");
function daidai($aa)
{
echo $aa;
}
这根本没有发出错误,而我期待 Warning: function daidai() expects at most 1 parameter, 3 given
令我困惑的是,这会按预期发出上述错误:
$Odate=new DateTime();
$sfasfasf=$Odate->setTime("23", "59", "30", "unexpected");
为什么?
那是因为setTime()
方法本身会对参数个数进行检查,如果你想在你的函数中使用它,你必须自己实现。
作为参考,您可以查看 the docs for func_num_args()
, func_get_arg()
and func_get_args()
来自文档:
No special syntax is required to note that a function is variadic; however access to the function's arguments must use func_num_args(), func_get_arg() and func_get_args().
因此,php 确实将每个函数都视为可变函数。