How to implement class methods in array_map's callable [duplicate]
Asked Answered
W

8

65

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?

Wame answered 24/3, 2011 at 16:18 Comment(0)
G
171

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.

Gonzalez answered 24/3, 2011 at 16:20 Comment(6)
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?Wame
@Justin: Take a look here: #49447Gonzalez
even statically works like array_map( @[ self, 'dash' ] )Swelling
How can you pass a parameter with the dash function?Cataphyll
@Cataphyll 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)Gonzalez
Since php 8.1, you can now use first-class callable syntax (wiki.php.net/rfc/first_class_callable_syntax). array_map($this->dash(...), $data);Crossbow
C
16

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 );
Cherellecheremis answered 24/2, 2010 at 21:6 Comment(0)
S
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.

Scottie answered 24/3, 2011 at 16:21 Comment(0)
U
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).

Uralic answered 24/3, 2011 at 16:20 Comment(1)
Thank you for you answer. I was very helpful do you have happen to know of any more articles about this subject? Thanks again,Wame
N
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.

Nonresistance answered 24/2, 2010 at 21:4 Comment(0)
A
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));
Acetabulum answered 16/9, 2021 at 9:7 Comment(0)
C
0
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
Cotenant answered 24/2, 2010 at 21:5 Comment(0)
M
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);

Mylohyoid answered 9/6, 2022 at 9:28 Comment(1)
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.Demmer

© 2022 - 2024 — McMap. All rights reserved.