比预期更多的参数:PHP 中没有错误? [英] More parameter than expected: no error in PHP?

查看:38
本文介绍了比预期更多的参数:PHP 中没有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看这段代码,我向自定义函数传递了比预期更多的参数:

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;
}

这根本不会发出错误,而我期待 警告:函数 daidai() 最多需要 1 个参数,给定 3 个

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");

为什么?

推荐答案

那是因为 setTime() 方法本身会检查参数的数量,如果你想在你的函数中使用它,你必须自己实现它.

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.

作为参考,您可以查看 文档func_num_args(), func_get_arg()func_get_args()

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

来自文档:

不需要特殊语法来说明函数是可变参数的;但是访问函数的参数必须使用func_num_args(),func_get_arg() 和 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 确实将每个函数都视为可变参数.

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

这篇关于比预期更多的参数:PHP 中没有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆