使用变量到 DataTables 服务器端列值格式化程序

Using Variable to DataTables Server-Side Column value Formatter

我想通过服务器端格式化程序在 DataTables 的每个单元格上传递 PHP 函数,但是 PHP 函数是动态的。这是示例代码:

$func_apply = $_GET['function_name']; // trim | strip_tags | md5 | ucwords

$columns[] = array(
'db'        => $field,
'dt'        => '3',
'formatter' => function( $d, $row ) {
                return $func_apply($d);
            }
);

但是这样我得到 PHP 错误未定义变量:$func_apply 如果我把 global $func_apply;在匿名函数中然后它给出致命错误:函数名称必须是字符串......

我已经使用 PHP 闭包解决了这个问题:

$func_apply = $_GET['function_name']; // trim | strip_tags | md5 | ucwords

$columns[] = array(
'db'        => $field,
'dt'        => '3',
'formatter' => function( $d, $row ) use ($func_apply) {
            return $func_apply($d);
        }
);