Can a method be used as an array_map function

前端 未结 3 627
遥遥无期
遥遥无期 2020-12-13 01:47

I want to do something like this:

class Cls {
  function fun($php) {
    return \'The rain in Spain.\';
  }
}

$ar = array(1,2,3);
$instance = new Cls();
print_r(         


        
3条回答
  •  醉话见心
    2020-12-13 01:54

    Yes, you can have callbacks to methods, like this:

    array_map(array($instance, 'fun'), $ar)
    

    see the callback type in PHP's manual for more info

提交回复
热议问题