0

A new class has been created for a PHP project as follows:

class Cleanse
{

    # trims leading and trailing spaces
    public static function trimmer($values)
    {       
        return is_array($values) ?
                    array_map('trimmer', $values) :
                    trim($values);
    }

}

However, when trying to use this functionality like so:

$values = Cleanse::trimmer($_POST);

the following warning message is returned: Warning: array_map() expects parameter 1 to be a valid callback, function 'trimmer' not found or invalid function name in (class file path) on line 41.

What is wrong with this code and/or this approach?

1 Answer 1

2

As trimmer is a static method of Cleanse, it should be

array_map('Cleanse::trimmer', $values) // PHP >= 5.2.3

or

array_map(array('Cleanse', 'trimmer'), $values) // PHP < 5.2.3

See Callbacks for the correct syntax to use for callbacks.

Sign up to request clarification or add additional context in comments.

Comments

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.