gpt4 book ai didi

syntax-error - PHP匿名函数在某些安装中导致语法错误

转载 作者:行者123 更新时间:2023-12-03 08:12:50 25 4
gpt4 key购买 nike

我有以下代码:

    $file_check_method_func = function($n) {
$n = absint($n);
if(1 !== $n) { $n = 0; }
return $n;
};
$valid['file_check_method'] = array_map($file_check_method_func, $input['file_check_method']);

这适用于我的PHP 5.3.5安装,但是当我在PHP 5.2.15安装上运行此代码时,我得到:
Parse error: syntax error, unexpected T_FUNCTION in /home/xxxx/public_html/xxxx/xxxxxxx/wp-content/plugins/wordpress-file-monitor-plus/classes/wpfmp.settings.class.php on line 220

第220行是上述代码的第一行。

所以我的问题是,我的代码中是否写错了某些东西会导致此错误?如果不是,是因为存在错误还是PHP 5.2.15中不支持该功能?如果是,那么我该如何编写上面的代码,以免产生错误?

上面的代码在一个类的函数中。

最佳答案

匿名功能是5.3中添加的功能

对于早期版本,创建一个命名函数并按名称引用它。例如。:

function file_check_method_func($n) {
$n = absint($n);
if(1 !== $n) { $n = 0; }
return $n;
}
$valid['file_check_method'] = array_map('file_check_method_func', $input['file_check_method']);

或在类(class)内:
class Foo {
protected function file_check_method_func($n) {
$n = absint($n);
if(1 !== $n) { $n = 0; }
return $n;
}
function validate($input) {
$valid = array();
$valid['file_check_method'] = array_map(array($this, 'file_check_method_func'), $input['file_check_method']);
return $valid;
}
}

我强烈建议不要依赖 create_function

关于syntax-error - PHP匿名函数在某些安装中导致语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6412032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com