blueman378 Posted October 14, 2009 Share Posted October 14, 2009 Hi guys, Im wondering i have a function which draws some stuff, anyway it takes 5 paramaters but the only one that changes when i call it in one place (over 20 times) is the text. So im wondering is there any way to say params = "param value 2, param value 3, param value 4, param value 5"; text = "text1"; function_name(text, params); instead of repeating all the info over and over, the function itself cannot be edited. i tried doing basically what i said above except it treats it as a single paramter, is there any way to make it treat it as all the params? Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/ Share on other sites More sharing options...
mikesta707 Posted October 14, 2009 Share Posted October 14, 2009 without being able to change the function, i'm afraid you can't alter how many, or how the parameters are passed Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936495 Share on other sites More sharing options...
blueman378 Posted October 14, 2009 Author Share Posted October 14, 2009 ah cheers, im suprised there isnt anything to take a string and treat it asif it was literally in that spot, oh well. Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936502 Share on other sites More sharing options...
mikesta707 Posted October 14, 2009 Share Posted October 14, 2009 you can use eval. something like params = "param value 2, param value 3, param value 4, param value 5"; text = "text1"; eval("function_name(".$text.", ".$params.")"); didnt test this though. and eval is not the best way to go. It will be a kind of shortcut for what you want to do, but eval is much slower than just normally parsing php, and since you are more or less being lazy there really isn't any substantial reason to use eval in this case. why can't you alter the function? Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936504 Share on other sites More sharing options...
blueman378 Posted October 14, 2009 Author Share Posted October 14, 2009 I cant alter it because the function is i a class i dont have control over (other coders are working on that class), and im not really being lazy, jsut trying to find ways to make the code tidier, Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936509 Share on other sites More sharing options...
mikesta707 Posted October 14, 2009 Share Posted October 14, 2009 the cons of using eval far out weight any pros you get from using eval. If you would be using eval alot, you will see a hige decrease in preformance. It may be ok if you are going to be doing a couple of times, but any more than 5 and I would stay away from it. also, if any user input ever goes into eval, you have to make sure you sanitze it as well as you can Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936511 Share on other sites More sharing options...
Mark Baker Posted October 14, 2009 Share Posted October 14, 2009 $baseParams = array("param value 2", "param value 3", "param value 4", "param value 5"); $text = "text1"; call_user_func_array("function_name", array_merge(array($text), $baseParams); Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936648 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 You could of course use the func_get_args() to get all the arguments of the function. That way you don't actually need to specify them at all, and just let your function handle them Take a look at a function I created here that combines mysql_real_escape_string and the sprintf function http://www.jaygilford.com/php/sprintf-and-mysql_real_escape_string-all-in-one-function/ Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936657 Share on other sites More sharing options...
trq Posted October 14, 2009 Share Posted October 14, 2009 You could of course use the func_get_args() to get all the arguments of the function. That way you don't actually need to specify them at all, and just let your function handle them Take a look at a function I created here that combines mysql_real_escape_string and the sprintf function http://www.jaygilford.com/php/sprintf-and-mysql_real_escape_string-all-in-one-function/ the OP doesn't have access to the function definition. Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936659 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 Oh, oops! My bad lol. Didn't realise that I would highly recommend against using eval for it also, as you don't want to be messing with stuff like that Surely you could use call_user_func in it's place Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936661 Share on other sites More sharing options...
trq Posted October 14, 2009 Share Posted October 14, 2009 Oh, oops! My bad lol. Didn't realise that I would highly recommend against using eval for it also, as you don't want to be messing with stuff like that Surely you could use call_user_func in it's place Yeah, call_user_func_array has already been suggested. Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936673 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 oh dear, I think I need to go back to sleep Link to comment https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/#findComment-936676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.