1

I am looking for something similar to the solution here >> Call function from string stored in a variable, however with one difference :

I would like to pass a parameter as well.

For example :

   function foo($myvar) {

   }


   $function = 'foo';
   $values = Array('myvar'=>1, 'myvar2'=>2);

   if(function_exists($function)) {
      // call the function using $function, 
      // and pass the value $values -- aka   $function($values)

   }

Any workable solution would be greatly appreciated. Ideally, I would like to use it with classes as follows :

   class bar {

   function foo($myvar) {

   }

   }


   $function = 'foo';
   $values = Array('myvar'=>1, 'myvar2'=>2);

   if(function_exists($function)) {
      // call the function using $function, 
      // and pass the value $values -- aka   $bar::$function($values)

   }
Community
  • 1
  • 1
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • I don't see where is the problem ? Can you give an example where it would cause a problem / – CMPS Apr 13 '14 at 04:57
  • It's not a problem --- My question is how can i pass a variable to a function where the function name is stored in a variable. Calling the function directly is not the issue. If you look at the SO link i added, it gives an answer for how to call a function in a string, but not if i wish to pass a variable to that function as well. -- so it's only 50% of the solution i need – Kraang Prime Apr 13 '14 at 05:02

3 Answers3

3

You can call a function on an object passing params, using variables, as follows:

$myBar = new bar(); // usually class names have uppercase first letters though
$myClassname = 'bar';
call_user_func(array($myBar, $function), $values); // method on object
call_user_func(array($myClassname, $function), $values); // class (static) method
call_user_func($myClassname.'::'.$function); // PHP 5.2.3 and higher
// if you had another function $function2 whose args were a list of parameters instead of an array:
call_user_func($myBar, $function2, $param1, $param2, $param3);

More info and examples at http://us1.php.net/call_user_func

RobP
  • 9,144
  • 3
  • 20
  • 33
  • can the class itself be called this way as well ? for example, in your example, the class is known and is static --- but something like `$foo = 'bar'; $mybar = new $foo;` ? or more specifically calling it statically like `$foo::$myfunction($params)` so all parts can be dynamic – Kraang Prime Apr 13 '14 at 05:05
  • Yes. http://stackoverflow.com/questions/4578335/creating-php-class-instance-with-a-string – RobP Apr 13 '14 at 05:06
  • Awesome --- thank you :) -- lol... i have to wait to accept the answer XD – Kraang Prime Apr 13 '14 at 05:07
  • you're welcome! hope it helps. (ahh the things we'll do for an upvote or a green checkmark ;) – RobP Apr 13 '14 at 05:08
  • :) ... I have a follow up question coming ... hehe... slightly more complex, but on topic with this -- basically, I am writing an event class (so I can raiseevents from other functions/classes/etc). PHP doesn't seem to have any real event based system. – Kraang Prime Apr 13 '14 at 05:22
  • Just giving heads up... question is being written now. – Kraang Prime Apr 13 '14 at 05:37
1

Does this helps ?

function process_function($function_param){
 $function_param = explode("|",$function_param);
   if(function_exists($function_param[0])) {
      call_user_func_array($function_param[0],$function_param[1]);
   }
}
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • 1
    Also a good solution. Allows for passing of the function name and one param. RobP answered first and is a bit more thorough, however bump for decent solution anyway :) Thank you. – Kraang Prime Apr 13 '14 at 05:11
1

I think you are looking for method_exist() which would check if object has particular method.

Here is your solution.

class bar {
   function foo($myarr){
        foreach($myarr as $val){
            echo $val ."<br />" ;
            //prints 1 and 2
        }
   }
}

$myfunc = new bar(); //creating object 
$values = Array('myvar'=>1, 'myvar2'=>2);

//checking object has method foo
if(method_exists($myfunc, 'foo')) {
    $myfunc->foo($values);
}
Suman Bogati
  • 6,289
  • 1
  • 23
  • 34
  • Thank you .... that is helpful as well. I wish we could just combine all 3 answers into a single solution. That doesn't really help with calling the function by string, but it does help with checking existence of function in a class. – Kraang Prime Apr 13 '14 at 05:17