32
    class theClass{
         function doSomeWork($var){
            return ($var + 2);
         }

         public $func = "doSomeWork";

         function theFunc($min, $max){
            return (array_map(WHAT_TO_WRITE_HERE, range($min, $max)));
         }
    }

$theClass = new theClass;
print_r(call_user_func_array(array($theClass, "theFunc"), array(1, 5)));
exit;

Can any one tell what i can write at WHAT_TO_WRITE_HERE, so that doSomeWork function get pass as first parameter to array_map. and code work properly.

And give out put as

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
)
hakre
  • 184,866
  • 48
  • 414
  • 792
Poonam Bhatt
  • 9,890
  • 16
  • 48
  • 70
  • It should work if you just wrote the function name there, "doSomeWork" – Amjad Masad Dec 28 '10 at 14:12
  • 1
    `array($this, $this->func)` will work, when passing an object you need to pass in an array where the first arg is the object and the second is the method within that object `array([object[],function])` – RobertPitt Dec 28 '10 at 14:45

3 Answers3

45

To use object methods with array_map(), pass an array containing the object and the objects method name. For same-object scope, use $this as normal. Since your method name is defined in your public $func property, you can pass func.

As a side note, the parentheses outside array_map() aren't necessary.

return array_map( [$this, 'func'], range($min, $max));
James Valeii
  • 442
  • 4
  • 13
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
  • 5
    Just had this issue now and this didn't work. What I had to do was pass it without the $this. i.e. array_map(array($this, 'func'). Not sure why it didn't work the first way. Found it here: https://stackoverflow.com/questions/5422242/array-map-not-working-in-classes – Dan W. Jan 04 '19 at 02:28
4

The following code provides an array of emails from an $users array which contains instances of a class with a getEmail method:

    if(count($users) < 1) {
        return $users; // empty array
    }
    return array_map(array($users[0], "getEmail"), $users);
psychoslave
  • 2,397
  • 2
  • 25
  • 41
0

I tried search for solution but it not works, so I created custom small map function that will do the task for 1 variable passed to the function it simple but will act like map

class StyleService {
  function check_css_block($str){
    $check_end = substr($str,-1) == ';';
    $check_sprator =  preg_match_all("/:/i", $str) == 1;
    $check_sprator1 =  preg_match_all("/;/i", $str) == 1;
    if ( $check_end && $check_sprator && $check_sprator1){
      return $str;
    } else {
      return false;
    }
  }

  function mymap($function_name, $data){
    $result = array();
    $current_methods = get_class_methods($this);
    if (!in_array($function_name, $current_methods)){
      return False;
    }
    for ($i=0; $i<count($data); $i++){
      $function_result = $this->{$function_name}($data[$i]);
      array_push($result, $function_result);
    }
    return $result;
  }


  function get_advanced_style_data($data)

  {
    return $this->mymap('check_css_block', $data);
  }
}

this way you can call a function name as string $this->{'function_name'}() in php

Mahmoud Magdy
  • 427
  • 5
  • 8