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)

   }
2
  • I don't see where is the problem ? Can you give an example where it would cause a problem / Commented Apr 13, 2014 at 4: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 Commented Apr 13, 2014 at 5:02

3 Answers 3

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 https://www.php.net/call_user_func

Sign up to request clarification or add additional context in comments.

6 Comments

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
Awesome --- thank you :) -- lol... i have to wait to accept the answer XD
you're welcome! hope it helps. (ahh the things we'll do for an upvote or a green checkmark ;)
:) ... 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.
|
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]);
   }
}

1 Comment

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.
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);
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.