Php 对所有数组成员调用对象成员函数

Php 对所有数组成员调用对象成员函数,php,arrays,callback,Php,Arrays,Callback,这可能是一个有点愚蠢的问题,它是一个方便而不是功能的问题 假设我们有一个类: class A { public function getId() { return rand(0,100); //For simplicity's sake } } 假设我们有一个A类型的对象数组,例如$array=[new A(),new A(),new A()] 通常,如果我想将所有对象的数组转换为其ID的数组,我会执行以下操作: $idArray = array_map(fun

这可能是一个有点愚蠢的问题,它是一个方便而不是功能的问题

假设我们有一个类:

class A { 
    public function getId() {
       return rand(0,100); //For simplicity's sake
    }
}
假设我们有一个
A
类型的对象数组,例如
$array=[new A(),new A(),new A()]

通常,如果我想将所有对象的数组转换为其ID的数组,我会执行以下操作:

$idArray = array_map(function ($a) { return $a->getId(); }, $array);
class A { 
    public function getId() {
       return rand(0,100); //For simplicity's sake
    }
}

class Container
{
    protected $elements;

    public function __construct($elements = array())
    {
        $this->elements = $elements;
    }

    public function map($method)
    {
        return array_map(function ($element) use ($method) {
            return $element->$method();
        }, $this->elements);
    }
}

$container = new Container([new A(), new A(), new A()]);

var_dump($container->map('getId'));
array(3) { [0]=> int(70) [1]=> int(32) [2]=> int(90) }
然而,这让我有一种强烈的愿望来简化它(我经常使用它)。我想要一个如下功能:

$idArray = magic_array_map('getId', $array);
我的问题是,有没有办法通过提供函数名而不是实际的回调来调用array_map(或类似的函数)?类似于
array\u map('count',$array)
但不是
strlen
提供成员方法名称


简言之,是否有这样一种内置PHP的方法,或者我必须实现自己的数组映射才能添加此功能?

您可以创建自己的帮助函数来执行您想要的操作

my_map_function($callback, $array) {
    return call_user_func_array(callback, $array);
}

my_callback_function($array) {
  //TODO
}

main() {
  $array = [1, 2, 3];
  $array = my_map_function('my_callback_function', $array);
}
类似这样的;)


这里有更多的

为什么不创建自己的函数并将其传递给
array\u map()?


在我看来,一个优雅的解决方案是创建一个容器类,其中包含一个map方法。比如:

$idArray = array_map(function ($a) { return $a->getId(); }, $array);
class A { 
    public function getId() {
       return rand(0,100); //For simplicity's sake
    }
}

class Container
{
    protected $elements;

    public function __construct($elements = array())
    {
        $this->elements = $elements;
    }

    public function map($method)
    {
        return array_map(function ($element) use ($method) {
            return $element->$method();
        }, $this->elements);
    }
}

$container = new Container([new A(), new A(), new A()]);

var_dump($container->map('getId'));
array(3) { [0]=> int(70) [1]=> int(32) [2]=> int(90) }
结果是:

$idArray = array_map(function ($a) { return $a->getId(); }, $array);
class A { 
    public function getId() {
       return rand(0,100); //For simplicity's sake
    }
}

class Container
{
    protected $elements;

    public function __construct($elements = array())
    {
        $this->elements = $elements;
    }

    public function map($method)
    {
        return array_map(function ($element) use ($method) {
            return $element->$method();
        }, $this->elements);
    }
}

$container = new Container([new A(), new A(), new A()]);

var_dump($container->map('getId'));
array(3) { [0]=> int(70) [1]=> int(32) [2]=> int(90) }

一种解决方案是将该技术应用到一个映射到阵列上的函数中:

function apply_member_function(string $method_name, array $array) :array {
    return array_map(function ($ob) {return $ob->$method_name();}, $array);
}
用法:

$array_of_as = [ new A(), new A(), new A() ];
$array_of_ids = apply_member_function('getId', $array_of_as);

我可能没有解释清楚。问题在于回调函数本身是数组中每个对象的成员函数。我会在问题中添加更多信息。我喜欢这个。这并不完全是我想要的,但它是一种优雅的面向对象方法。根据我在公司的个人经验,我建议不要这样做,制作我们自己的类似数组的类。创建自己的类(而不是php数组)的缺点是a)您失去了与数组一起工作的内置功能的便利性,[您可以通过实现
ArrayAccess、Countable
和可选的
IteratorAggregate
]来恢复大部分功能]b)维护人员未来的困惑:有时您不可避免地要使用数组,有时您要使用自己的类。在像php这样的松散类型语言中,这是不好的;他们只是通过将lambda函数直接嵌入
数组映射
调用来更方便地实现(一次性使用)。OP所寻找的是一种不必每次都编写自己的函数的方法。也就是说,下一次他们想要调用不同的函数时,因此提到传递成员函数的名称。[如果您多次调用同一个成员函数,那么另一种解决方案是使用一个函数来执行整个任务:
function getIds(array$array\u of_a){return array\u map(function($a){return a$a->getId();},$array_of_a));}
。用法:
$ids=getIds($array);
]