PHP修改代码以避免匿名功能 [英] PHP modify code to avoid anonymous functions

查看:87
本文介绍了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天全站免登陆