0

How can I call a function in a class that depends on a user's input.

So if the user sent 2011 I want to call the method $record->show_2011()

if they sent the input 2012 I want to call the method $record->show_2012()

else I want to call $record->show()

How do I pass in the user's input into the method name to call it?

2
  • Probably best to map incoming variables to method / function names. Commented Nov 22, 2013 at 17:58
  • 2
    Can't you just pass it into a function as a param instead? Commented Nov 22, 2013 at 17:59

3 Answers 3

2
$functionToCall = 'show_' . $userInput; $record->$functionToCall(); 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to put the function name in a variable first, e.g. $function_name = 'set'.$request->getFirstProperty(); and then $record->$function_name();

Comments

0

You can also do it this way:

$record->{'show'}(); $record->{'show_' . $variableThatHoldsTheRestOfFunctionName}(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.