How to implement class methods in array_map's callable [duplicate]
I am trying to create a class to handle arrays but I can't seem to get arra开发者_开发问答y_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?
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.
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 );
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.
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).
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.
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));
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
//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);
For multidimensional arrays (any arrays):
$data = array_map('decode'), $data);
function decode($data)
{
if (is_array($data)) {
foreach ($data as &$value) {
if (is_array($value)) {
$value = decode($value);
} else {
$value = html_entity_decode($value);
}
}
} else {
$data = html_entity_decode($data);
}
return $data;
}
For multidimensional arrays (any arrays) in Class:
$data = array_map(array($this,'decode'), $data);
private function decode($data)
{
if (is_array($data)) {
foreach ($data as &$value) {
if (is_array($value)) {
$value = $this->decode($value);
} else {
$value = html_entity_decode($value);
}
}
} else {
$data = html_entity_decode($data);
}
return $data;
}
精彩评论