Mardoxx Posted August 31, 2009 Share Posted August 31, 2009 I got the idea when helping here: http://www.phpfreaks.com/forums/index.php/topic,267209.0.html I've created this very sketchy function for pushing string variables into a function: (hopefully you can see how it works) <?php function arg_splitter($input,$arg_count=false,$args_num='args_num') { $split = preg_replace('/\$|\"/','', preg_split('/\"[\s,]+\$/',$input)); /* First this splits the inputted string on any number of commas * or spaces inbetween the characters " and $ * then it replaces all $ and " characters with nothing * my regex skillz suck. */ foreach ($split as $key=>$val) { $split[$key] = explode("=",$val); $assocarray[$split[$key][0]] = $split[$key][1]; //creates associative array so you can use extract on it } if($arg_count) { $assocarray[$args_num]=count($assocarray); } return $assocarray; } function newfn($inputs) { extract(arg_splitter($inputs,true,'arg_count')); // Only line needed to be added if(isset($override) && !is_null($override)) { echo "O MAYN, WE GOT OVERRIDDEN!!!!\n<br />"; echo "\$override = $override\n<br />"; return; } echo "\$a = $a\n<br />"; echo "\$b = $b\n<br />"; echo "\$c = $c\n<br />"; echo "\$d = $d\n<br />"; echo "\$arg_count = $arg_count\n<br /><br />"; } echo "Test 1: \n<br />"; newfn('$a="apple",$b="banana", $c="carrot",$d="date"'); echo "Test 2: \n<br />"; newfn('$override="apple, banana, carrot, duck"'); ?> gives an output of: Test 1: $a = apple $b = banana $c = carrot $d = date $arg_count = 4 Test 2: O MAYN, WE GOT OVERRIDDEN!!!! $override = apple, banana, carrot, duck Any easier/other way of doing this? thanks Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/ Share on other sites More sharing options...
Philip Posted August 31, 2009 Share Posted August 31, 2009 Why not just pass the data as an array to begin with, wouldn't that just be easier? function newfn(array $data) { foreach($data as $key => $value) { // do whatever... } // whatever here } newfn(array('a'=>'apple', 'b'=>'banana', 'c'=>'carrot', 'd'=>'date')); Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909438 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Author Share Posted August 31, 2009 lol yeah then just a eval("$$key = $value;"); (just incase you had newfn(array('a'=>'apple', 'b'=>'array(\'44\'=>\'o hai\')')); or something ) hehe so simple! thanks Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909447 Share on other sites More sharing options...
Philip Posted August 31, 2009 Share Posted August 31, 2009 Or just do what you did in your other one, instead of eval.... extract Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909457 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Author Share Posted August 31, 2009 can extract handle arrays and stuff? Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909614 Share on other sites More sharing options...
KevinM1 Posted August 31, 2009 Share Posted August 31, 2009 can extract handle arrays and stuff? What do you think extract() is for? Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909616 Share on other sites More sharing options...
trq Posted August 31, 2009 Share Posted August 31, 2009 Or.... function test() { print_r(func_get_args()); } test(1, 2, 'foo', array(1,5,'blah'), 'what'); Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909617 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Author Share Posted August 31, 2009 no, because then you have to remember what order you entered them in Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909631 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Author Share Posted August 31, 2009 can extract handle arrays and stuff? What do you think extract() is for? extract(array('a' = > array('a' => 'not this'), 'b' => 'bus')) doesn't work //edit fix`d idk why i was evaling... <?php function newfn(array $data) { foreach($data as $key => $value) { $$key = $value; //works fine } // whatever here } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172511-function-with-different-number-of-arguments/#findComment-909636 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.