Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass a callback WITH arguments in PHP

Tags:

php

callback

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.

like image 703
Emanuel Mocean Avatar asked Jan 16 '14 01:01

Emanuel Mocean


2 Answers

$that = $this;

$wrapper = function() use($that) {
    return $that->my_function_name('arg1', 'arg2');
};
like image 89
zerkms Avatar answered Nov 12 '22 22:11

zerkms


There is a function called call_user_func_array.

call_user_func_array([$this, 'method'], $args);
like image 26
roel Avatar answered Nov 12 '22 21:11

roel