coplan Posted March 6, 2007 Share Posted March 6, 2007 I'm writing a modular system, and I want to be able to dynamically call specific functions based on the $_GET[] variables. All the modules will have a standard naming protocol for functions. For example: ?_module_main() where '?' is the name of the module. Now, say the url of the page is http://whatever.com/?go=news, I want to be able to call the main news function, or news_module_main(). I need to dynamically call that function by replacing the first part of the function name. What is the best way to handle that? I've already tried: $content = $_GET['go']._module_main(); // and $content = $_GET['go'].'_module_main()'; Both result in undesired effects. Your thoughts? Link to comment https://forums.phpfreaks.com/topic/41501-dynamic-function-namescalls/ Share on other sites More sharing options...
boo_lolly Posted March 6, 2007 Share Posted March 6, 2007 i'd use a switch statement: <?php switch($_GET['go']){ case "news": //call your function here break; case "something else": //call something else here break; default: break; } ?> Link to comment https://forums.phpfreaks.com/topic/41501-dynamic-function-namescalls/#findComment-201050 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 try this snippet <?php function f_A () { echo "<p>A called</p>"; } function f_B () { echo "<p>B called</p>"; } if (isset($_GET['func'])) { $fname = 'f_' . $_GET['func'] ; $fname(); } ?> <form> Select function A or B <br><br> <input type='radio' name='func' value='A'> A<br> <input type='radio' name='func' value='B'> B<br> <input type='submit' name='action' value='Call function'> </form> Link to comment https://forums.phpfreaks.com/topic/41501-dynamic-function-namescalls/#findComment-201057 Share on other sites More sharing options...
coplan Posted March 7, 2007 Author Share Posted March 7, 2007 Barand, your method works well for me. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/41501-dynamic-function-namescalls/#findComment-201615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.