View: Call a user function given by the first parameter in PHP

  1. 5 months ago by spirit and saved by 1 other
    1. // Call a class method outside the class
    2. call_user_func(array($myclass, $mymethod), $args);
    3.  
    4. // Call a class method inside the class
    5. call_user_func(array($this, $mymethod), $args);
    6. //or
    7. $this->{$mymethod}($args);

2 comments about "Call a user function given by the first parameter in PHP"

  1. What if there is more than a single parameter to give to $mymethod ?
    palleas on March 25, 2008
  2. It's just a truncated example. For multiple parameters, it's just call_user_func(array($myclass, $mymethod), $arg1, $arg2, ...); or call_user_func_array(array($myclass, $mymethod), array($arg1, $arg2, ...)); . See http://www.php.net/manual/en/function.call-user-func.php and http://www.php.net/manual/en/function.call-user-func-array.php
    spirit on March 25, 2008