Zimi Posted August 9, 2008 Share Posted August 9, 2008 A friend of mine has a problem, he wants to be able to store function names inside variable and then be able to use the functions that are stored in the variables. How can he do that? Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Use $mvar(); However why would want to store function names in a variable? Make sure these variables are not set by the user otherwise you could cause security exploits in your code. Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612456 Share on other sites More sharing options...
Zimi Posted August 9, 2008 Author Share Posted August 9, 2008 Use $mvar(); However why would want to store function names in a variable? Make sure these variables are not set by the user otherwise you could cause security exploits in your code. Don't know why he need it Oo Can you please explain me about $mvar()? google and php.net gives me nothing =p Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612461 Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612463 Share on other sites More sharing options...
.josh Posted August 9, 2008 Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Hey wow I didn't know you could do that; learn somethin' new every day I was just gonna suggest eval() Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612465 Share on other sites More sharing options...
cooldude832 Posted August 9, 2008 Share Posted August 9, 2008 In a primitive example it doesn't show much use. the only realistic example is in a need to have a function be executed based off a user's input but even then a switch can be the same or better assuming a reasonable number of choices. Why use eval?? Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612466 Share on other sites More sharing options...
Zimi Posted August 9, 2008 Author Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Oh, I got it, how stupid am I XD Thanks ^^ Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.