Question

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;
}    
Was it helpful?

Solution

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;
}

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top