2

I can't kind of make out the first return statement, can anybody help to explain how it works? the array_map accept a function for the first arg, but here is an array. and how does array(&$this, '_trimData') work? thanks for explaining.

private function _trimData($mParam)
{       
    if (is_array($mParam))
    {
        return array_map(array(&$this, '_trimData'), $mParam);
    }

    $mParam = trim($mParam);

    return $mParam;
}    

3 Answers 3

3

This is a recursive function. _trimData calls itself if the parameter passed to it was an array.

array(&$this, '_trimData') is a callback to the current object's method _trimData.

The entire method could really be replaced with:

private function _trimData($mParam)
{ 
    array_walk_recursive($mParam, 'trim');
    return $mParam;
}
1
  • Got it,,,your function is much easier to understand. thanks for the explanation. Commented Jan 8, 2012 at 16:59
1

It is callback: $this->_trimData() (_trimData of object $this)

0

A bit further of an explanation about how array(&$this, '_trimData') acts as a callback, despite looking like an array:

A PHP function is passed by its name as a string... A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. PHP: Callbacks/Callables

So in this case, the object is &$this and the method is _trimData, and making it into an array is one way PHP allows you to pass it as a callback into array_map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.