Alex Rivera | Logout

Can a method be used as an array_map function

Asked 2009-07-03T01:48:38.610
88

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(array_map('$instance->fun', $ar));
               // ^ this won't work

but the first argument to array_map is supposed to be the name of the function. I want to avoid writing a wrapper function around $instance->fun, but it doesn't seem like that's possible. Is that true?

Edit
Report

1 Answer

46

You can also use

array_map('Class::method', $array) 

syntax.

answered 2012-12-21T22:12:36.033

Your Answer