将带有匿名函数的代码转换为 PHP 5.2 [英] Converting code with Anonymous functions to PHP 5.2

查看:20
本文介绍了将带有匿名函数的代码转换为 PHP 5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 PHP 5.3 代码,用于构建要传递给视图的数组.这是我的代码.

I have some PHP 5.3 code which builds an array to be passed to a view. This is the code I have.

# Select all this users links.
$data = $this->link_model->select_user_id($this->user->id);
if (count($data) > 0) {
    # Process the data into the table format.
    $table = array
    (
        'properties' => array
        (
            'delete_link_column' => 0,
        ),
        'callbacks' => array
        (
            # Callback for the name link.
            function($value) {
                return sprintf('<a href="/links/view/name/%s">%s</a>', $value, $value);
            },
            # Callback for the category link.
            function($value) {
                return sprintf('<a href="/category/view/name/%s">%s</a>', $value, $value);
            },
            # Callback for the creation date.
            function($value) {
                return date('jS M Y', $value);
            },
            # Callback for the delete link.
            function($value) {
                return sprintf('<a href="links/delete/name/%s">delete</a>', $value);
            },
        ),
        'columns' => array
        (
            'name', 'category', 'creation date',
        ),
        'data' => array
        (

        ),
        'sorting' => array
        (
            'sort' => false,
        ),
    );

但是问题是我不能在 PHP 5.2 中使用匿名函数,这是我必须上传这个功课的服务器.视图需要定义回调函数才能调用它们.

However the problem is that I cannot use anonymous functions in PHP 5.2, which is the server I must upload this schoolwork. The view requires callback functions to be defined so it can call them.

将此 PHP 代码转换为不使用匿名函数的最简洁方法是什么?谢谢.

What would be the neatest way to convert this PHP code to not using anonymous functions? Thanks.

推荐答案

你可以像这样调用其中一个函数:

You could call one of those function like so:

$func = $callbacks[0];
$func();

这也适用于 create_function() 并为命名函数使用字符串,如下所示:

Which also works with create_function() and using strings for named functions like so:

function test() {
  echo "test";
}
$func = 'test';
$func();

$func = create_function('' , 'echo "test 2"; ');
$func();

此外,如果调用是使用 call_user_func 你可以使用 array($object, 'func_name') 来调用一个对象或一个类的公共方法 array('Class_Name', 'func_name'):

Also, if the calling is done using call_user_func you can use array($object, 'func_name') to call a public method on an object or a class with array('Class_Name', 'func_name'):

class Test {
  public static function staticTest() { echo "Static Test"; }
  public function methodTest() { echo "Test"; }

  public function runTests() {
    $test = array('Test', 'staticTest');
    call_user_func($test);

    $test = array($this, 'methodTest');
    call_user_func($test);
  }
}

$test = new Test();
$test->runTests();

这篇关于将带有匿名函数的代码转换为 PHP 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆