0

我正在尝试替换 CMS 中已弃用的“create_function”。我已经为这个话题找到了很多答案。然而,我使用的补丁导致了更多的麻烦,我对这个级别的 PHP 函数的工作并不是很坚定。因此,如果有人可以在这里帮助我,我会非常高兴。

我变了

$list->setColumnFormat('active', 'custom', create_function(
  '$params',
  'global $I18N;
   $list = $params["list"];
   return $list->getValue("active") == 1 ? $I18N->msg("yes") : $I18N->msg("no");'
));

$list->setColumnFormat('active', 'custom', function($params) {
  global $I18N; 
  $list = $params["list"]; 
  return $list->getValue("active") == 1 ? $I18N->msg("yes") : $I18N->msg("no");
});

但是然后 - 而不仅仅是“弃用”消息 - 我得到

可恢复的致命错误:第 155 行的 [filename] 中的类 Closure 的对象无法转换为字符串

第 155 行是:

trigger_error('rexCallFunc: Using of an unexpected function var "'.$function.'"!');

var_dump($function) 给出:

object(Closure)#16 (1) { ["parameter"]=> array(1) { ["$params"]=> string(10) "" } }

如果我将第 155 行注释掉,我还会得到:

警告:call_user_func() 期望参数 1 是一个有效的回调,函数“未找到”或第 163 行 [相同文件名] 中的函数名无效

第 163 行是:

return call_user_func($func, $params);

var_dump($func) 给出:

string(0) "" 

有谁知道这是什么以及如何解决?

4

1 回答 1

0

create_function 返回一个名为“匿名”函数的字符串,而不是匿名函数。如果您想避免更改setColumnFormat()方法,则需要创建一个命名函数而不是匿名函数,并将该名称作为setColumnFormat()可以在call_user_func().

function patchFunction($params) {
    global $I18N; 
    $list = $params["list"]; 
    return $list->getValue("active") == 1 ? $I18N->msg("yes") : $I18N->msg("no");
}

$list->setColumnFormat('active', 'custom', 'patchFunction');
于 2020-02-21T16:53:40.663 回答