Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters with call_user_func?

Tags:

php

Is there a way to call a function using call_user_func and pass it parameters? For instance I have

function test($args)
{
    echo "I DID IT!";
}

however I cannot call this function because it has a paramter which is not being passed by

call_user_func("test");

is there a way to call the function and provide parameters to it? (for instance, the ability to pass in a list arguments)? Any help is greatly appreciated!

like image 246
Serguei Fedorov Avatar asked Aug 01 '12 21:08

Serguei Fedorov


2 Answers

If you were to read the documentation you would see that call_user_func() accepts a variable number of arguments:

call_user_func('test', 'argument1', 'argument2');

You can also use call_user_func_array('callback', array('array','of','arguments')).

like image 187
Francis Avila Avatar answered Oct 30 '22 07:10

Francis Avila


Use call_user_func_array, you can supply a list of parameters as array.

like image 20
Dreen Avatar answered Oct 30 '22 07:10

Dreen