Skip to content
Advertisement

pass a callback WITH arguments in PHP

I have a PHP library function expecting a callback with no arguments. I know I can pass an object’s method with array($this , 'my_function_name') but how can I give parameters to the my_function_name? I have found a solution using create_function but from PHP manual I see it has security issues.

Advertisement

Answer

$that = $this;

$wrapper = function() use($that) {
    return $that->my_function_name('arg1', 'arg2');
};
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement