php - How to implement class methods in array_map's callable
Get the solution ↓↓↓Solution:
You are specifyingdash
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.
Answer
Solution:
When using a class method as a callback for functions likearray_map()
andusort()
, 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 );
Answer
Solution:
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 wantarray_map(array($this,'dash'), $data)
instead.
Answer
Solution:
It must read
$this->classarray = array_map(array($this, 'dash'), $data);
Thearray
-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 asarray('ClassName, 'methodName')
or as a string like that:'ClassName::methodName'
(this works as of PHP 5.2.3).
Answer
Solution:
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.
Answer
Solution:
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));
Answer
Solution:
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
Answer
Solution:
//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);
Answer
Solution:
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;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.