0

I got a database table that has a class-name, method and parameters saved in it:

class | method | parameters
---------------------------
User  | logIn  | para1

I now need to call it in my script. I have "User" saved in $class, "logIn" saved in $method and "para1" saved in $para:

$class->$method($para);

Is this anyhow possible?

sjagr
  • 15,983
  • 5
  • 40
  • 67
Philip
  • 1,068
  • 3
  • 12
  • 21
  • 1
    Did you try `$class->{$method}($para);`? Your original way to do it would still work as well... – sjagr Jan 05 '15 at 15:46
  • 1
    possible duplicate of [Dynamic class method invocation in PHP](http://stackoverflow.com/questions/251485/dynamic-class-method-invocation-in-php) – sjagr Jan 05 '15 at 15:49
  • Have you instantiated an instance of the `User` class and saved _that_ into `$class` instead of just the string "User"? – Patrick Q Jan 05 '15 at 15:49

1 Answers1

0

Since this solved OP's problem, and as per this answer (awaiting duplicate closure for this problem) and PHP's standard methodology of variable variables, you can use:

$class->{$method}($para);

However $class->$method($para); should also work as well.

Community
  • 1
  • 1
sjagr
  • 15,983
  • 5
  • 40
  • 67