PHP 抑制函数参数中的错误

发布于 2024-12-26 04:44:45 字数 346 浏览 1 评论 0原文

来自 https://stackoverflow.com/a/55191/547210
的扩展 我正在创建一个验证函数来检查字符串变量的多个属性,这些属性可能已设置,也可能未设置。 (检查的属性之一)
我试图对该函数执行的操作是接收表单中未知数量的参数(见下文),并抑制可能因传递未设置的变量而导致的错误。
我使用 func_get_args() 接收诸如 validate([ mix $... ] ) 之类的变量
上一篇文章提到可以通过引用传递,现在当变量像这样隐式传递时可以吗?

Extension from https://stackoverflow.com/a/55191/547210
I am creating a validating function to check several attributes of string variables, which may or may not have been set. (One of the attributes which is checked)
What I am trying to do with the function is receive arguments an unknown number of arguments in the form (See below), and suppress errors that may be caused by passing an unset variable.
I'm receiving the variables like validate([ mixed $... ] ) by using func_get_args()
The previous post mentioned that it was possible by passing by reference, now is this possible when the variables are passed implicitly like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2025-01-02 04:44:46

如果传递的变量未在调用范围中设置,则 func_get_args() 返回的数组将在传递该变量的位置包含一个 NULL 值,并且将触发错误。这个错误不是在函数代码本身触发的,而是在函数调用时触发的。因此,无法从函数代码中抑制此错误。

考虑一下:

function accepts_some_args () {
  $args = func_get_args();
  var_dump($args);
}

$myVar = 'value';
accepts_some_args($notSet, $myVar);

/*
  Ouput:

  Notice: Undefined variable: notSet in...
  array(2) {
    [0]=>
    NULL
    [1]=>
    string(5) "value"
  }
*/

正如您所看到的,变量名 notSet 出现在错误中,告诉我们错误是在调用者的范围内触发的,而不是在被调用者的范围内。

如果我们想解决这个错误,我们可以这样做:

accepts_some_args(@$notSet, $myVar);

...并用邪恶的 @ 运算符作为变量名的前缀,但更好的解决方案是以不同的方式构建我们的代码,所以我们可以这样做我们自己检查:

function accepts_some_args ($args) {
  var_dump($args);
}

$myVar = 'value';

$toPassToFunction = array();
$toPassToFunction[] = (isset($notSet)) ? $notSet : NULL;
$toPassToFunction[] = (isset($myVar)) ? $myVar : NULL;

accepts_some_args($toPassToFunction);

/*
  Ouput:

  array(2) {
    [0]=>
    NULL
    [1]=>
    string(5) "value"
  }
*/

If you pass a variable that is not set in the calling scope, the array returned by func_get_args() will contain a NULL value at the position where the variable was passed, and an error will be triggered. This error is not triggered in the function code itself, but in the function call. There is, therefore, nothing that can be done to suppress this error from within the code of the function.

Consider this:

function accepts_some_args () {
  $args = func_get_args();
  var_dump($args);
}

$myVar = 'value';
accepts_some_args($notSet, $myVar);

/*
  Ouput:

  Notice: Undefined variable: notSet in...
  array(2) {
    [0]=>
    NULL
    [1]=>
    string(5) "value"
  }
*/

As you can see, the variable name notSet appears in the error, telling us that the error was triggered in the caller's scope, not that of the callee.

If we want to counter the error, we could do this:

accepts_some_args(@$notSet, $myVar);

...and prefix the variable names with the evil @ operator, but a better solution would be to structure our code differently, so we can do the checks ourselves:

function accepts_some_args ($args) {
  var_dump($args);
}

$myVar = 'value';

$toPassToFunction = array();
$toPassToFunction[] = (isset($notSet)) ? $notSet : NULL;
$toPassToFunction[] = (isset($myVar)) ? $myVar : NULL;

accepts_some_args($toPassToFunction);

/*
  Ouput:

  array(2) {
    [0]=>
    NULL
    [1]=>
    string(5) "value"
  }
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文