More parameter than expected: no error in PHP?

血红的双手。 提交于 2019-12-20 04:27:14

问题


Please see this code, where I pass more parameters than expected to a custom function:

error_reporting(E_ALL);
ini_set("display_errors", 1);

daidai("asassa", "asgfasfas", "asassa");

function daidai($aa)
{
    echo $aa;
}

This doesn't emit an error at all, while I was expecting Warning: function daidai() expects at most 1 parameter, 3 given

What puzzles me is that this emits said error as expected:

$Odate=new DateTime();
$sfasfasf=$Odate->setTime("23", "59", "30", "unexpected");

Why?


回答1:


That is because the setTime() method itself performs a check on the number of parameters, if you want it in your functions, you have to implement it yourself.

For reference you might look into the docs for func_num_args(), func_get_arg() and func_get_args()

From the docs:

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().

Hence, php really treats every function as a variadic one.



来源:https://stackoverflow.com/questions/28188454/more-parameter-than-expected-no-error-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!