【问题标题】:Patch for create_function() causes problems in PHP 7.3.6create_function() 的补丁导致 PHP 7.3.6 出现问题
【发布时间】:2020-02-21 16:12:55
【问题描述】:

我正在尝试替换 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) "" 

有人知道这是怎么回事以及如何解决吗?

【问题讨论】:

    标签: php patch create-function


    【解决方案1】:

    create_function returns a string 带有“匿名”函数的名称,而不是匿名函数。如果您想避免更改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');
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2013-09-13
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多