4

In Python I can do this:

def f(a, b, c):
    print a, b, c

f(*[1, 2, 3])

How do you say this in PHP?

wes
  • 1,577
  • 1
  • 14
  • 32

1 Answers1

9

Use call_user_func_array:

call_user_func_array('f', array(1, 2, 3));

If you wanted to call a class method, you'd use array($instance, 'f') instead of 'f' and if it was a static class function you'd use array('ClassName', 'f') or 'ClassName::f'. See the callback type docs for details about that.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • this is ugly, but in the question, he knows all the parameters, so you can also `$f = 'fname'; $f(1,2,3);` and, if it's an object method, you can `$o->{$f}(1,2,3);` – zanlok Nov 24 '10 at 01:00
  • 3
    My eyes, the goggles, they do nothing! Reminds me why I now code in Python when I can... – fmark Nov 24 '10 at 01:40
  • He wanted to pass the arguments from an array though (which is exactly what a *array arg in python does). – ThiefMaster Nov 24 '10 at 01:54