PHP修改代码避免匿名函数 [英] PHP modify code to avoid anonymous functions

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

问题描述

我找到了一些解决我遇到的排序问题的方法,但代码使用了 PHP 中的匿名函数.我使用的是 5.2.17 版本,我认为不支持匿名函数.

I've found some solutions to a sorting problem I've been having, but the code uses anonymous functions in PHP. Im using version 5.2.17 and I believe anonymous functions are not supported.

我将如何更改以下代码块以便我可以在 PHP 5.2.17 中使用它们

How would I change the following blocks of code so I can use them in PHP 5.2.17

$keys = array_flip($order);

usort($items, function($a, $b) use($keys)
{
    return $keys[$a['id']] - $keys[$b['id']];
});

来自 PHP 按其他数组排序多维数组

$sorted = array_map(function($v) use ($data) {
    return $data[$v - 1];
}, $order);

来自 PHP - 按另一个数组对多维数组进行排序

更新:问题之一是我不确定如何使用变量 $a、$b 和 $v.所以我不确定如何创建普通函数,从而绕过匿名函数.

UPDATE: One of the problems is I'm not sure how the variables $a, $b and $v are used. So I'm not sure how to create normal functions, thus bypassing the anon functions.

推荐答案

两个匿名函数都使用 use 子句将变量传递到局部作用域.

Both of the anonymous functions make use of the use clause to pass variables into the local scope.

您可以使用对象方法实现相同的目的,其中对象将这些变量作为属性.

You can achieve the same with object methods in which the objects have those variables as properties.

在对象方法中,您可以访问这些.

Inside the object method you then can access these.

$sorted = array_map(function($v) use ($data) {
    return $data[$v - 1];
}, $order);

一个示例性的映射对象可能如下所示:

An exemplary mapping object then could look like:

class MapObj
{
    private $data;
    public function __construct($data) {
        $this->data = $data;
    }

    public function callback($v) {
        return $this->data[$v - 1];
    }
}

如您所见,它具有相同的功能,但只是用 PHP 5.2 语法编写.

As you can see it has the same functionality but just written in PHP 5.2 syntax.

它的用法:

$map = new MapObj($data);
$callback = array($map, 'callback');
$sorted = array_map($callback, $order);

这就是它的工作原理.对象方法的回调总是以array的形式编写,有两个成员,第一个是对象实例,第二个是对象方法的名称.

And that's how it works. Callbacks for object methods are always written in form of an array with two members, the first one is the object instance, and the second one is the name of the object method.

当然你可以扩展这个,把映射函数放到映射对象中,这样就更直接了:

Naturally you can extend this an put the mapping function into the mapping object, so it's more straight forward:

class MapObj
{
    ...

    public function map(array $order) {
        $callback = array($this, 'callback');
        return array_map($callback, $order);
    }
}

新用法:

$map = new MapObj($data);
$sorted = $map->map($order);

如您所见,这可能会使使用更加直接.我必须承认,我的方法命名在这里并不是很出色,所以我为您的改进留出了一些空间.

As you can see this might make the usage a bit more straight forward. I must admit, my method naming is not really brilliant here, so I leave some room for your improvements.

另一个好处是,您可以将回调方法的可见性设为私有.

Another benefit is, you can make the visibility of the callback method private then.

将要在回调中使用的数据作为参数传递给映射函数的情况.那是因为你写了你已经有一个你想要使用的类,但是你不能触及构造函数.所以给出的例子有点短.

The situation with passing the data to work with in the callback as a parameter to the mapping function. That is because you wrote you already have a class that you want to make use of, but you can not touch the constructor. So the given example is a bit short.

这是另一个没有使用构造函数的例子,我把它去掉了:

Here is another example without using the constructor, I removed it:

class MyObj
{
    private $data;

    private function callback($v) {
        return $this->data[$v - 1];
    }

    public function map($order, $data) {
        $callback = array($this, 'callback');
        $this->data = $data;
        return array_map($callback, $order);
    }
}

如您所见,不再需要构造函数来传递 $data,而是将其作为附加参数传递到 map() 方法中.用法:

As you can see, the constructor is not needed any longer to pass the $data, but instead it's just passed into the map() method as an additional parameter. Usage:

$myObj = new MyObj(....); // somewhere.

// later on:

$myObj->map($order, $data);

// could be also:

$this->map($order, $data);

如您所见,如何设置私有成员变量取决于您.做适合工作的事情.

As you can see, how you set the private member variable is up to you. Do what fits the job.

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

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