66

I am trying to create a class to handle arrays but I can't seem to get array_map() to work in it.

$array = [1,2,3,4,5,6,7,8,9,10];
class test {
    public $values;

    public function adding($data) {
        $this->values = array_map($this->dash(), $data);
    }

    public function dash($item) {
        return '-' . $item . '-';
    }

}

var_dump($array);

$test = new test();
$test->adding($array);

// Expected: -1-,-2-,-3-,-4-... 
var_dump($test->values);

This outputs

array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }

Warning: Missing argument 1 for test::dash(), called in [...]\arraytesting.php on line 11 and defined in [...]\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in [...]\arraytesting.php on line 11 NULL

What am I doing wrong or does this function just not work inside classes?

0

8 Answers 8

173

You are specifying dash as the callback in the wrong way.

This does not work:

$this->classarray = array_map($this->dash(), $data);

This does:

$this->classarray = array_map([$this, 'dash'], $data);

Read about the different forms a callback may take here.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the quick replay. I got it working and thanks for you help. I was just wondering do you happen to have more article about callback and how to specify if correctly?
@Justin: Take a look here: stackoverflow.com/questions/48947/…
even statically works like array_map( @[ self, 'dash' ] )
How can you pass a parameter with the dash function?
@Nielsapp you cannot directly, you must pass in a callable that will effectively bind the arguments that you want, e.g. array_map(function($item) { return $this->dash($item, 'additional argument'); }, $data)
|
17

When using a class method as a callback for functions like array_map() and usort(), you have to send the callback as two-value array. The 2nd value is always the name of the method as a string. The 1st value is the context (class name or object)

// Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );

Comments

3

array_map($this->dash(), $data) calls $this->dash() with 0 arguments and uses the return value as the callback function to apply to each member of the array. You want array_map(array($this,'dash'), $data) instead.

Comments

2

It must read

$this->classarray = array_map(array($this, 'dash'), $data);

The array-thing is the PHP callback for a object instance method. Callbacks to regular functions are defined as simple strings containing the function name ('functionName'), while static method calls are defined as array('ClassName, 'methodName') or as a string like that: 'ClassName::methodName' (this works as of PHP 5.2.3).

1 Comment

Thank you for you answer. I was very helpful do you have happen to know of any more articles about this subject? Thanks again,
1

array_map takes a callback as its first parameter.

And a callback to a static method is written like this :

array('classname', 'methodname')


Which means that, in your specific case, you'd use :

array_map(array('stripSlashesRecursive', ''), $value);


For more informations about callbacks, see this section of the PHP manual : Pseudo-types and variables used in this documentation - callback.

Comments

1

In case the class belongs to a different namespace, you need to use the complete namespaced class name. Below is an example using a CakePHP Utility class:

This will not work:

array_map(array('Inflector', 'humanize'), $some_array));

This will work:

array_map(array('Cake\Utility\Inflector', 'humanize'), $some_array));

Comments

0
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...

Comments

0

//Regular functions: array_map('MyFunction', $array);

//static functions in a class: array_map(array('MyClass', 'MyFunction'), $array);

//functions from an object: array_map(array($this, 'MyFunction'), $array);

//functions from an parent class array_map(array($this, 'parent::MyFunction'), $array);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.