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

查看:78
本文介绍了使用匿名函数将代码转换为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中使用匿名函数,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天全站免登陆