0

can I call a method that is represented as string?

Example:

$function = '$this->myfunction($myparam)';

How could I call myfunction with myparam? Possible?

Thanks :)

hakre
  • 193,403
  • 52
  • 435
  • 836
EOB
  • 2,975
  • 17
  • 43
  • 70
  • The easiest way to do it is with `eval()`, but dangerous if that string comes from an outside source. – alex May 23 '12 at 12:03

1 Answers1

1

You can use the call_user_func(); function of PHP.

eg.,

function callMe($message)
{
echo "My Function was called with arg:".$message;
}

and you can call it like

call_user_func("callMe","MyMessage");

and it will return any value, that the function returns.

Also, if you want to call with multiple arguments or array , you can use

call_user_func_array()

Hope you can get a detailed implementation of these in www.php.net

Kris
  • 8,680
  • 4
  • 39
  • 67