stompeers Posted November 30, 2007 Share Posted November 30, 2007 Hi folks! This is my first post in PHP Freaks, I hope it's up to snuff. Does anyone know if you can define a function name using a variable? For example: $var = 'my_function_name'; function $var() { } I know you can't do that verbatim, as I've tried, but I was wondering if there is some workaround that would be valid? Thanks! Chris Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/ Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 $teng ='funcY'; function funcY(){ echo 'this is teng'; } $teng(); you can have something like that Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402568 Share on other sites More sharing options...
stompeers Posted November 30, 2007 Author Share Posted November 30, 2007 Thanks, I know that one is possible, but you still have to define the function knowing the function's name. I would like to define a function dynamically,without first knowing it's name. Is there any way to do that? Thanks! Chris Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402592 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 Why not just test it? Also, why would you want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402596 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 Don't limit his options, he wants what he wants Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402598 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 dynamic creating of functions ? hmm i dont see any instance that you can use that perhaps dynamic calling might be useful but dynamic creating of function?? Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402603 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 Don't limit his options, he wants what he wants I'm not limiting his options, I'm actually trying to do the opposite. I'm trying to figure out why in the world they would want to do that as I can't think of a single reason, and then maybe I can direct them to a better method. Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402615 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 Don't limit his options, he wants what he wants I'm not limiting his options, I'm actually trying to do the opposite. I'm trying to figure out why in the world they would want to do that as I can't think of a single reason, and then maybe I can direct them to a better method. yah meeeen im with you why would you waste your time in worthless things like this .. Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402618 Share on other sites More sharing options...
btherl Posted November 30, 2007 Share Posted November 30, 2007 You can create functions without a name with create_function(). But for what you want you will need eval(): $newfunc = 'function foo() { print "foo\n"; }'; eval($newfunc); foo(); Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402620 Share on other sites More sharing options...
PHP_PhREEEk Posted November 30, 2007 Share Posted November 30, 2007 You can create functions without a name with create_function(). But for what you want you will need eval(): $newfunc = 'function foo() { print "foo\n"; }'; eval($newfunc); foo(); That still creates the function with a known name ahead of time... I think he wants (if this even works... lol) $someVar = 'someName'; $newfunc = 'function' . $someVar . '() { print "foo\n"; }'; eval($newfunc); $someVar(); Now I have to go test that... haha PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402657 Share on other sites More sharing options...
PHP_PhREEEk Posted November 30, 2007 Share Posted November 30, 2007 PHP didn't find the humor in that code at all... heheh PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402662 Share on other sites More sharing options...
PHP_PhREEEk Posted November 30, 2007 Share Posted November 30, 2007 Missed a space... the code below prints foo $someVar = 'someName'; $newfunc = 'function ' . $someVar . '() { print "foo\n"; }'; eval($newfunc); $someVar(); PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402663 Share on other sites More sharing options...
btherl Posted November 30, 2007 Share Posted November 30, 2007 I assumed the OP would know that he could alter the string before calling eval() Quote Link to comment https://forums.phpfreaks.com/topic/79498-can-you-define-a-functions-name-with-a-variable/#findComment-402684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.